iOS学习之路iOS Developer程序员

UITableViewCell嵌套UIScrollView

2016-03-09  本文已影响2318人  南方_H

直接上代码
自定义Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _scroll = [[UIScrollView alloc]init];
        _scroll.delegate = self;
        
        
        [self addSubview:_scroll];
    }
    
    return self;
}

-(void)setUpCellWithArray:(NSArray *)array WithIndexPath:(NSIndexPath *)indexPath
{
    CGFloat xbase = 10;
    CGFloat width = 130;
    
    [self.scroll setScrollEnabled:YES];
    [self.scroll setShowsHorizontalScrollIndicator:NO];
    
    for(int i = 0; i < [array count]; i++)
    {
        UIImage *image = [array objectAtIndex:i];
        UIView *custom = [self createCustomViewWithImage: image inIndex:i whichTheColumn:indexPath.section];
        
        custom.backgroundColor = [UIColor redColor];
        custom.tag = i;
        
        [self.scroll addSubview:custom];
        [custom setFrame:CGRectMake(xbase, 0, width, ITEM_H)];
        xbase += 10 + width;
         
    }
    
    [self.scroll setContentSize:CGSizeMake(xbase, self.scroll.frame.size.height)];
    
    self.scroll.delegate = self;
}

-(UIView *)createCustomViewWithImage:(UIImage *)image inIndex:(int)index whichTheColumn:(NSInteger)column
{
    /**
     cell中的每一项 item参数
     */
    UIView *custom = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 130, ITEM_H)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 130, 120)];
    [imageView setImage:image];
    
    [custom addSubview:imageView];
 
    custom.tag = index;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,120, 130, ITEM_H - 120)];
    label.text = [NSString stringWithFormat:@"%d  %ld",index,(long)column];
    
    label.numberOfLines = 2;
    
    label.font = [UIFont systemFontOfSize:16];

    [custom addSubview:label];
    
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap: wichTheColumn:)];
    
    [custom addGestureRecognizer:singleFingerTap];
    
    return custom;
}


//添加的手势点击
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer wichTheColumn:(NSInteger)column{
    
    UIView *selectedView = (UIView *)recognizer.view;
    
    int index = selectedView.tag;
    
    if([_cellDelegate respondsToSelector:@selector(cellSelectedWishIndex: wichTheColumn:)])
        [_cellDelegate cellSelectedWishIndex:index wichTheColumn:column];
     
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    _scroll.frame = CGRectMake(0, 10, WIDTH, ITEM_H);
   

控制器代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//看这里,为什么不去重用cell
    NSString *cellID = [NSString stringWithFormat:@"cell%d",indexPath.section];
    
    HTHomeScrolCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HTHomeScrolCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    } 
    NSLog(@"cell在第%ld个分区",(long)indexPath.section);
    
    [cell setUpCellWithArray:images WithIndexPath:indexPath];
    
    [cell.scroll setFrame:CGRectMake(cell.scroll.frame.origin.x, cell.scroll.frame.origin.y, cell.frame.size.width, 164)];
     
    cell.cellDelegate = self;
    
    return cell;

}

好了,效果如下:


IMG_4724.jpg

注意一下cell重用引发的BUG。

上一篇下一篇

猜你喜欢

热点阅读