UITableViewCell嵌套UICollectionView

  • UITableView和UICollection的嵌套使用
    • 1.在控制器中创建TableView,设置数据源和代理
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrData = [NSMutableArray arrayWithObjects:@"末",@"将",@"于",@"禁",@"愿",@"为",@"曹",@"家",@"世",@"代",@"赴",@"汤",@"蹈",@"火", nil];
    [self setupTabView];
}
-(void)setupTabView{
    self.tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    self.tabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    [self.view addSubview:self.tabView];
}
  • 2.自定义TableViewCell:TableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.arr = [NSMutableArray array];
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flow];
    self.collView.contentInset = UIEdgeInsetsMake(5, 10, 5, 10);
    self.collView.backgroundColor = [UIColor lightGrayColor];
    [self.collView registerClass:[CellColl class] forCellWithReuseIdentifier:@"cell000"];
    self.collView.delegate = self;
    self.collView.dataSource = self;
    [self addSubview:self.collView];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.collView.frame = self.bounds;
}

  • 3.实现UICollectionView的协议方法
#pragma mark --UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CellColl *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell000" forIndexPath:indexPath];
    cell.lbl.text = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, self.bounds.size.height-20);
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    cell.layer.mask = shape;
}

Demo:https://github.com/BeyondScience/TableView-collectionView

你可能感兴趣的:(UITableViewCell嵌套UICollectionView)