我在 TableView 中使用了 Collection View ,并在来自后端的 Collection View 中填充了数据。现在,当我重新加载数据时,它不会以我试图显示的方式向我显示 UI,我希望我的 UI 看起来像这样,
但是当我运行我的应用程序并打开 VC 时,我的表格 View 看起来像这样,
数据不是以水平方式进入的,而是复制了 Collection View 的单元格。这是我的 Collection View 代码,用于在其中填充数据。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
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
}
这是包含 Collection View 的 TableView 的代码,
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
cell.categoryCollectionView.delegate = self
cell.categoryCollectionView.dataSource = self
cell.categoryCollectionView.reloadData()
return cell
}
我想在 UI 中显示我的数据,就像我问题中的第一张图片一样。
最佳答案
你可能需要
1-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // change this to 1
}
2-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = knowledgeTableView.dequeueReusableCell(withIdentifier: "KnowledgeCell", for: indexPath) as! KnowledgeDetailTVC
cell.categoryCollectionView.tag = indexPath.section // add this
cell.categoryCollectionView.delegate = self
cell.categoryCollectionView.dataSource = self
cell.categoryCollectionView.reloadData()
return cell
}
3-
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categoryArray[collectionView.tag].blogArray.count // use tag here
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "familyCell", for: indexPath) as! FamilyCVC
cell.categoryLbl.text = categoryArray[collectionView.tag].blogArray[indexPath.row].articleTitle
let imageUrl = categoryArray[collectionView.tag].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
}
关于ios - 快速在 TableView 内的 Collection View 中显示内容时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57169296/