还是项目需求,需要在一个列表实现多个图片滑动的效果,所以就用了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点击事件");
}
到这里就结束了,希望能帮到你!!
复制代码