ios - 快速在 TableView 内的 Collection View 中显示内容时出现问题

标签 ios swift

我在 TableView 中使用了 Collection View ,并在来自后端的 Collection View 中填充了数据。现在,当我重新加载数据时,它不会以我试图显示的方式向我显示 UI,我希望我的 UI 看起来像这样, enter image description here

但是当我运行我的应用程序并打开 VC 时,我的表格 View 看起来像这样, enter image description here

数据不是以水平方式进入的,而是复制了 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/

相关文章:

iOS Sprite Kit - SKSpriteNode 的 .centerRect 属性不起作用

ios - AVAudioEngine:一次启动一组 AVAudioPlayerNode

cocoa-touch - 如何在 Split View iPad 应用程序中操作母版?

swift - 我们如何在 Swift 中使用 NSDate 对动态 UITable 进行排序和分段(排序)?

ios - Swift 3 Facebook 登录

ios - 使用 CoreGraphics 在 IOS 中使用交叉影线?

ios - UIsearchbar 不将数据返回到表

ios - 使用 NotificationCenter Swift 4.0 不会调用观察者

swift - 获取 Vapor 中的 URI 片段

ios - UITableView 未加载 Firebase 快照数据。