【问题标题】:Assertion failure in UICollectionView inside UITableViewCellUITableViewCell 内的 UICollectionView 中的断言失败
【发布时间】:2020-02-17 04:14:42
【问题描述】:

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

委托和数据源已连接到主情节提要中的 HomeViewController。此外,还给出了单元格的名称。

编译程序时没有错误,但在运行代码时得到一个期望。

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 TopCategoryCollectionID 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格

当我试图通过只显示 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:)"

【问题讨论】:

    标签: ios uitableview exception uicollectionview swift5


    【解决方案1】:

    问题在于使用集合视图注册单元格。集合视图无法使具有“TopCategoryCollectionID”标识符的单元格出列。您需要对集合视图说,我将使用此单元格作为集合视图单元格。

    您需要将TopCategoryCollectionID 单元格注册到集合视图。

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

    如果你在storyboard中使用过cell,那么就不需要再注册cell了。

    编辑

    由于您的 tableview 单元格中有集合视图,因此数据源和集合视图的委托必须在各自的单元格中实现。

    您需要将集合视图代码转移到表格视图单元格中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2012-09-17
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多