UITableViewCell嵌套UIScrollView

作者: 南方_H | 来源:发表于2016-03-09 23:01 被阅读2318次

    直接上代码
    自定义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。

    相关文章

      网友评论

      • AI衣衣:亲,源码发我邮箱哈:1577003764@qq.com
      • sunmumu1222:亲 能给我发下源码吗?我的qq邮箱335089101@qq.com
      • 188baaafd568:麻烦发一下源码2511290702@qq.com
      • 飞不飞:可以发一下源码么? 941133705@qq.com, 并且请教一下 UITableViewCell中嵌套UIScrollView UIScrollView 再次嵌套UITableView 怎么解决手势冲突问题
      • Fooler:复用那个bug 怎么解决啊
      • nenhall:Cell 必须重用啊,得解决那个bug才是
        nenhall:@我是一朵向阳花 問博主:smile:
        Fooler:解决 了吗?我也遇到了那个bug了,里面所有的都实例化了两份,,,
      • EchoZuo:zuoqianheng@qq.com 发现源码,学习下。谢谢。
      • 2ed665e4e2b9:@selector(handleSingleTap: wichTheColumn:)第二个参数带不出来吧
        2ed665e4e2b9:我得到的column总是1
      • 389378e6cd0b:2754699695@qq.com 能麻烦发下源码谢谢
      • 倒骑毛驴看风月:591050673@qq.com,发现源码谢谢!!!
      • 5b6bbd1c2abe:亲 能给我发下源码吗?我的qq邮箱116217291@qq.com
      • 遛遛食:能把源码发下学习一下吗?467388603@qq.com 谢谢
        南方_H:@遛遛食 发了 :blush: 有什么问题及时联系。Demo还有点不足,我会抓紧修复
      • jsonzhou:发下源码 学习下 :smile:
        南方_H:@jsonzhou 嗯,发了。 :blush:
        jsonzhou:@南方_H 1181446613@qq.com 多谢
        南方_H:可以留下邮箱吗?我发你。:blush:

      本文标题:UITableViewCell嵌套UIScrollView

      本文链接:https://www.haomeiwen.com/subject/iahtlttx.html