【问题标题】:Issue in Showing Collection View in Table View Cell in iOS在 iOS 的表格视图单元格中显示集合视图的问题
【发布时间】:2019-06-14 16:00:00
【问题描述】:

我有一个表格视图,并且我在单元格内放置了一个集合视图,我从 API 获取数据,并且我正在传递给表格视图和集合视图的数据,但是当我运行应用程序时,它会因此错误而崩溃,

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[Lawon.KnowledgeVC collectionView:numberOfItemsInSection:]:无法识别的选择器发送到实例 0x7fa90af0cbc0

我的表格视图代码,

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
 }
}

这是我的表格视图单元格类,我有一个疯狂的出口来收集视图并在其中填充数据并调用它的委托和数据源,

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) {

}

}

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

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

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

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

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

    在这种情况下不会抛出编译时错误,这是在 IB 中设置委托和数据源的一个大问题

    【讨论】:

    • 我在表格视图单元格类中为集合视图创建了 IBOutlet,并在代码中提到的表格视图单元格类中调用其委托和数据源。 @Sh_Khan
    • 你的回答后还有问题吗?另外,您在 vc 中没有包含该集合的原型表单元格吗?
    • 知识VC中如何制作集合视图的IBOutlet,集合视图会放在表格视图的单元格内吗?它向我显示错误。 @Sh_Khan
    • outlet 无论如何都应该在单元格内,您需要将集合的所有委托和数据源方法复制到 vc 并将 collectionView 的委托和数据源分配给 cellForRowAt 内的 vc 或保留它因为它现在在您发布问题时分配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多