Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell内UICollectionView中的断言失败_Ios_Uitableview_Exception_Uicollectionview_Swift5 - Fatal编程技术网

Ios UITableViewCell内UICollectionView中的断言失败

Ios UITableViewCell内UICollectionView中的断言失败,ios,uitableview,exception,uicollectionview,swift5,Ios,Uitableview,Exception,Uicollectionview,Swift5,我的代码在UITableViewCell中有一个UICollectionView。我有两组不同的数组,名为TopBrandArray和TopCategoryArray,它显示在collectionView中表的两个不同单元格中 代理和数据源已连接到主情节提要中的HomeViewController。此外,还提供了单元格的名称 编译程序时没有错误,但在运行代码时得到预期值 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符T

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

代理和数据源已连接到主情节提要中的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的引用不明确。\uUuNumberOfItemsInSection:

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

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

let nib = UINib(nibName: "TopCategoryCollectionID", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "TopCategoryCollectionID")
若你们已经在故事板中使用了单元格,那个么就不需要再次注册单元格了

编辑

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


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

注释不用于扩展讨论;这段对话已经结束。