【发布时间】:2026-02-03 16:05:01
【问题描述】:
我有动态 TableView,在原型单元格中有 CollectionView。我创建了 UITableViewCell 的子类,并为 TableView 原型单元格添加了自定义单元格。我还添加了 UICollectionVeiwCell 作为 CollectionView 的 CustomCell。
它在 Storyboard 中的样子:
下面我用来创建场景的代码:
//-=-=-=-==-=-=--==-=-=-=-=-=-=-=-=--=-=-=-TableView methods-=-=-=-=--=-=-=-=-=-=-=-=
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 15;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[myCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
return cell;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-==-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-==
//-=-=-=-==-=-=--==-=-=-=-=-=-=-=-=--=-=-=-CollectionView methods-=-=-=-=--=-=-=-=-=-=-=-=
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 7;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"CVCell";
CVCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if (indexPath.row == 0) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 1) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 2) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 3) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 4) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 5) {
cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row];
}
if (indexPath.row == 6) {
cell.cellTxtFld.text =@"LAST";
}
return cell;
}
它在模拟器中的样子:
我的问题是,我怎样才能直接访问每个 collectionView?例如,我有 15 个数组,我希望第一个 collectionView(在第一个 TableView 的行中)按 0 索引、第二个 - 1 索引等初始化。我该怎么做?
【问题讨论】:
标签: ios objective-c uitableview uicollectionview