部分之间的多个自定义TableviewCell上的多个自定义UICollectionView

时间:2016-10-25 10:29:05

标签: ios swift uitableview uicollectionview swift3

多个自定义TableviewCell上的多个自定义UICollectionView

我想要的: 多个TableviewCell上的多个自定义UICollectionView。

 这些因人而异。

我遵循了这个教程:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

但我无法弄清楚问题发生的原因,

错误显示,

  

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法使类型的视图出列:具有标识符ProductCell的UICollectionElementKindCell - 必须为标识符注册一个nib或类或连接一个故事板中的原型单元'

我从cellForRowAt注册了Nib文件, 当然我在Nib文件上添加了标识符。

来自TableView的方法,就像这样

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    switch indexPath.section{
    case 0:

        let cell = tableView.dequeueReusableCell(withIdentifier: product, for: indexPath) as! ProductTableViewCell
        cell.collectionView.register(UINib(nibName: "ProductCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ProductCell")

        cell.collectionView.viewWithTag(0)
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ShowDataTableViewCell
        cell.collectionView.register(UINib(nibName: "ShowDataCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
        cell.collectionView.viewWithTag(1)
        return cell

    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ShowDataTableViewCell

        cell.collectionView.register(UINib(nibName: "ShowDataCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")
        cell.collectionView.viewWithTag(99)

        return cell
    }

}

这不起作用,我也尝试从TableViewCell注册nib文件,

但它也不起作用。

或者numberOfItemsInSection方法会出现问题吗?

extension TableChildFoodViewController : UICollectionViewDelegate, UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{


        print("tag: \(collectionView.tag)")


        switch collectionView.tag{
        case 0:

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as! ProductCollectionViewCell
            return cell

        case 1:            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! ShowDataCollectionViewCell
            return cell


        default:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! ShowDataCollectionViewCell
            return cell
        }


    }

}

如果可能,你可以给我建议吗?

TableChildFoodViewController.swift

1 个答案:

答案 0 :(得分:0)

解决,

我应该采用这种方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

not,cell.collectionView.viewWithTag(0)

我修复了,cell.collectionView.tag = indexPath.section

多数民众赞成工作了!

相关问题