gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:41:24 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com