【问题标题】:Dynamic TableView with CollectionView in prototype cell原型单元格中带有 CollectionView 的动态 TableView
【发布时间】: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


    【解决方案1】:

    您可以将collectionView 作为Table 视图的自定义视图类中的一个属性,然后对其进行处理。从自定义单元格中的方法传递数组以获取自定义单元格数组。然后您将能够直接访问每个 tableView 的集合视图。

    编辑:更具体:

    您有一个 tableView 单元格myCustomCell。在其中创建UICollectionView 的属性,您可以将其委托设置为仅控制器。在myCustomCell 中创建一个方法。可以在cellForAtIndexpath 中调用并将委托设置为self。现在,您可以根据需要访问您的UICollectionView

    【讨论】:

    • 你能说得更具体些吗?
    • 您的建议导致编译失败。我用简单而愚蠢的方式做到了——在 CellForItemForIndexPath 中使用计数器。但我不知道我能不能通过我的方式获得一些 TextField 的 textField.text 。你能帮帮我吗?
    • 可以设置tag,然后获取。你可以和我聊天@mrsamkitjain@gmail.com
    最近更新 更多