iOS tableview嵌套collectionview

还是项目需求,需要在一个列表实现多个图片滑动的效果,所以就用了tableview嵌套collectionview来实现,废话不多说,直接上代码

这是tableView的创建,collectionview直接写在了自定义的tableviewCell里
#pragma make --tableView代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ZQWMyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellId];
    ZQWMyOrderModel *model = self.dataArr[indexPath.row];
    cell.model = model;
    return cell;

}

这里是自定义tableviewcell的方法

//collectionView
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(100, 100);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, lineView.y + 1, myWidth - 30, 100) collectionViewLayout:layout];
        layout.minimumInteritemSpacing = 3;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[ZQWMyOrderCollectionCell class] forCellWithReuseIdentifier:collectionViewCellId];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.collectionView];

//tableviewcell的model 的set方法里获取每个collectionview的数据
-(void)setModel:(ZQWMyOrderModel *)model{

NSData *data = [[NSData alloc] initWithData:[model.orderItemList dataUsingEncoding:NSUTF8StringEncoding]];
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

//这里的数组一定不能懒加载,否则数据就会出问题
    self.dataArr = [NSMutableArray array];

    for (NSDictionary *modelDict in dataArray) {
        ZQWMyOrderItemModel *model = [ZQWMyOrderItemModel new];
        [model setValuesForKeysWithDictionary:modelDict];
        [self.dataArr addObject:model];
    }
    
//对你的collectionView进行刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.collectionView reloadData];
        
    });
}

剩下的就是正常的赋值,事件的处理了
#pragma make --collectionView代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    
    return self.dataArr.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ZQWMyOrderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellId forIndexPath:indexPath];
    ZQWMyOrderItemModel *model = self.dataArr[indexPath.row];
    
    cell.model = model;
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"collectionView点击事件");

}

到这里就结束了,希望能帮到你!!

复制代码

转载于:https://juejin.im/post/5a311bc26fb9a045263b9670

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值