先创建左边的tableView和右边的collectionView
UITableView *tableView = [[UITableView alloc]init];
self.tableView = tableView;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = ControllerBgColor;
tableView.frame = CGRectMake(0, 64, ScreenWidth / 4, ScreenHeight);
tableView.showsVerticalScrollIndicator = NO;
tableView.separatorStyle = UITableViewCellSelectionStyleNone;
tableView.tableFooterView = [[UIView alloc]init];
[self.view addSubview:tableView];
[tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CategoryCell class]) bundle:nil] forCellReuseIdentifier:@"CategoryCell"];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 0.5;
layout.minimumInteritemSpacing = 0.5;
CGFloat w = (ScreenWidth - ScreenWidth / 4) / 3 - 0.5;
CGFloat h = 44;
layout.itemSize = CGSizeMake(w, h);
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
self.collectionView = collectionView;
collectionView.frame = CGRectMake(tableView.frame.size.width, 64, (ScreenWidth - ScreenWidth / 4), ScreenHeight);
collectionView.backgroundColor = ControllerBgColor;
[collectionView registerClass:[UserCell class] forCellWithReuseIdentifier:@"UserCell"];
[self.view addSubview:collectionView];
点击tableViewCell更新collectionView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryModel_result *result = self.categoryModel.result[indexPath.row];
self.collectionCellArray = result.children;
[self.collectionView reloadData];
}
collectionView代理实现
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (self.collectionCellArray == 0) {
CategoryModel_result *result = self.categoryModel.result[section];
return result.children.count;
} else {
return self.collectionCellArray.count;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UserCell" forIndexPath:indexPath];
if (self.collectionCellArray == 0) {
CategoryModel_result *result = self.categoryModel.result[indexPath.section];
CategoryModel_result_children *children = result.children[indexPath.row];
cell.label.text = children.name;
} else {
CategoryModel_result_children *children = self.collectionCellArray[indexPath.row];
cell.label.text = children.name;
}
return cell;
}
网友评论