ios - 在 iOS 的 TableView 单元格中显示 Collection View 的问题

标签 ios swift uitableview uicollectionview

我有一个 TableView ,我在单元格中放置了一个 Collection View ,我从 API 获取数据并将这些数据传递给 TableView 和 Collection View ,但是当我运行该应用程序时它崩溃并出现此错误,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Lawon.KnowledgeVC collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7fa90af0cbc0

我的表格 View 代码,

extension KnowledgeVC : UITableViewDelegate,UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return categoryArray.count
}

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


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 35
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = Bundle.main.loadNibNamed("KnowledgeHeaderTVC", owner: self, options: nil)?.first as! KnowledgeHeaderTVC

    headerView.categoryNameLbl.text = categoryArray[section].catName
    headerView.articlesLbl.text = "\(categoryArray[section].blogArray.count)" + "articles"

    return headerView
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = knowledgeTableView.dequeueReusableCell(withIdentifier: "KnowledgeCell", for: indexPath) as! KnowledgeDetailTVC

    cell.categoryArray = categoryArray

    return cell
 }
}

这是我的 TableView 单元格类,其中我有用于收集 View 和填充数据并调用其委托(delegate)和数据源的疯狂导出,

class KnowledgeDetailTVC: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var categoryCollectionView : UICollectionView!
var categoryArray = [Category]()

override func awakeFromNib() {
    super.awakeFromNib()

    self.categoryCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print(categoryArray[section].blogArray.count)
    return categoryArray[section].blogArray.count
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "familyCell", for: indexPath) as! FamilyCVC

    cell.categoryLbl.text = categoryArray[indexPath.section].blogArray[indexPath.row].articleTitle
    let imageUrl = categoryArray[indexPath.section].blogArray[indexPath.row].imageUrl!
    print(imageUrl)
    cell.categoryImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "person.jpeg"))
    //            cell.bgView.layer.masksToBounds = true
    //            cell.bgView.layer.shadowColor = UIColor.black.cgColor
    //            cell.bgView.layer.shadowOpacity = 3
    //            cell.bgView.layer.shadowOffset = CGSize(width: -1, height: 1)
    //            cell.bgView.layer.shadowRadius = 2
    //            cell.bgView.layer.shouldRasterize = true

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

}

最佳答案

您可以将原型(prototype)单元格中 collectionView 的委托(delegate)和数据源设置为 IB 中的 KnowledgeVC,而实现在单元格内,但您应该将它们设置在 awakeFromNib

self.categoryCollectionView.delegate = self
self.categoryCollectionView.dataSource = self
self.categoryCollectionView.reloadData()

另外最好在 cellForRowAt 中刷新它以避免出队问题

cell.categoryArray = categoryArray
cell.categoryCollectionView.reloadData()

There is no compile time error thrown in that case and that is a big problem of setting the delegate and dataSource in IB

关于ios - 在 iOS 的 TableView 单元格中显示 Collection View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56601708/

相关文章:

ios - UITableView 在滚动时得到空项目

iphone - 如何从 UITableView 中删除部分 (iPhone/iPad)

ios - Swift Model 对象应该调用 API 吗?

android - 处理每个用户的时区和 DST 的任务调度(运行备份)

ios - 展开一个可选值 nil

swift - 如何删除数组中的第一个元素并将其用作 Swift 中 reduce 函数的初始值?

objective-c - 如何使用 UIScrollView 或 UITableView 设计和创建 GridView

iphone - iOS PDF 表格和报告

iOS:将澳大利亚语言设置为语言并将澳大利亚设置为区域时本地化为 en-GB

ios - 从 UITabBarController 内部呈现 UINavigationController