我有动态 TableView,在原型(prototype)单元格中有 CollectionView。我创建了 UITableViewCell 的子类,并为 TableView 原型(prototype)单元格添加了自定义单元格。我还添加了 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 索引等初始化。我怎样才能做到这一点?
最佳答案
您可以将 collectionView 作为 Table View 的自定义 View 类中的属性,然后对其进行处理。从自定义单元格中的方法传递数组以获取自定义单元格数组。然后您将能够直接访问每个 tableView 的 Collection View 。
编辑:更具体:
你有一个 tableView 单元格 myCustomCell
.创建 UICollectionView
的属性在其中,您只能将其委托(delegate)设置为 Controller 。在 myCustomCell
中创建一个方法.可以在 cellForAtIndexpath
中调用并将委托(delegate)设置为自我。现在,您可以访问您的 UICollectionView
根据您的需要
关于ios - 原型(prototype)单元格中带有 CollectionView 的动态 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21755044/