ios - UITableViewCell 内的 UICollectionView 断言失败

标签 ios uitableview exception uicollectionview swift5

我的代码在 UITableViewCell 中有一个 UICollectionView。我有两组不同的数组,名为 TopBrandArray 和 TopCategoryArray - 它们显示在 collectionView 内表的两个不同单元格中。

委托(delegate)和数据源已连接到主 Storyboard 中的 HomeViewController。此外,还给出了单元格的名称。

编译程序时没有错误,但运行代码时出现期望。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier TopCategoryCollectionID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

当我尝试通过仅显示 tableviewcell 的一个单元格来运行时,代码工作正常。但是,如果添加更多单元格,则会弹出上述错误。在这里,我添加代码:

@IBOutlet weak var homePageTable: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.homePageTable.reloadData()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

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

    if indexPath.row == 0{
        let cell = tableView.dequeueReusableCell(withIdentifier: "TopCategoryID", for: indexPath) as! TopCategoriesTableViewCell
        cell.tablecollection1.reloadData()
        return cell
    }
    else{
        let cell = tableView.dequeueReusableCell(withIdentifier: "BrandTableID", for: indexPath) as! BrandsTableViewCell
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 162
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0{
        return topCategoryArray.count
    }
    else{
        return topBrandArray.count
    }

}

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

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopCategoryCollectionID", for: indexPath) as? TopCategoriesCollectionViewCell{
        cell.cat_image.image = UIImage(named: topCategoryArray[indexPath.row].image)
        print(topCategoryArray[indexPath.row].name)
        cell.cat_value.text = topCategoryArray[indexPath.row].name
    return cell
    }
    else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BrandsCollectionID", for: indexPath) as! BrandsCollectionViewCell
        cell.layer.borderWidth = 1.5
        cell.layer.borderColor = UIColor.gray.cgColor
        cell.layer.cornerRadius = 4
        cell.brandimage.image = UIImage(named: topBrandArray[indexPath.row].image)
        return cell
    }
}
}

由于它要求为标识符注册一个 Nib 或一个类,所以我不知道应该在代码内部的哪里编写代码,因为当我在 viewDidload() 中编写代码时,会弹出一个错误,显示“对成员collectionView(_:numberOfItemsInSection:)"

最佳答案

问题在于使用 Collection View 注册单元格。 Collection View 无法使具有“TopCategoryCollectionID”标识符的单元格出队。您需要对 Collection View 说我将使用此单元格作为 Collection View 单元格。

您需要将 TopCategoryCollectionID 单元格注册到 Collection View 。

let nib = UINib(nibName: "TopCategoryCollectionID", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "TopCategoryCollectionID")

如果你已经在storyboard中使用过cell,那么无需再次注册cell。

编辑

由于表格 View 单元格内有 Collection View ,因此 Collection View 的数据源和委托(delegate)必须独立于相应的单元格实现。

您需要将 Collection View 代码传输到 TableView 单元格中。

关于ios - UITableViewCell 内的 UICollectionView 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58480093/

相关文章:

iPhone:自定义 UITableViewCell 具有多个响应点击的区域?

Swift - 在 TableView 中更改标题部分的垂直对齐方式

python - 一行异常处理

c# - 是否存在指示非法对象状态的内置 .NET 异常?

ios - SwiftUI:@State 已修改但在阅读之后的行时看似未更改

ios - 如何在执行 segue 之前执行按钮动画

ios - 是否可以手动调用 UITableview 的 editActionsForRowAt?

ios - 将多个 block 参数传递给方法

ios - iPad 语音识别 - 开发人员访问?

硬编码参数时的 Java 异常处理