首页 文章

在原型单元格中使用CollectionView的动态TableView

提问于
浏览
2

我有动态TableView,在原型单元格中有CollectionView . 我创建了UITableViewCell的子类,并为TableView原型单元添加了Custom单元格 . 我还为CollectionView添加了UICollectionVeiwCell作为CustomCell .

它在Storyboard中的样子:
enter image description here

下面的代码我用来创建我的场景:

//-=-=-=-==-=-=--==-=-=-=-=-=-=-=-=--=-=-=-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;
    }

它在模拟器中的样子如何:

enter image description here

我的问题是,我怎样才能直接访问每个collectionView?例如,我有15个数组,我想要第一个collectionView(在第一个TableView的行中)init由0-index,second-1 index等等 . 我怎样才能做到这一点?

1 回答

  • 0

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

    编辑:更具体:

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

相关问题