在表格视图内的集合视图中快速显示内容时出现问题

时间:2019-07-23 17:21:22

标签: ios swift

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

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

数据不是水平传送的,它会复制收集视图的单元格。这是我的“收集”视图的代码,用于在其中填充数据。

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
}

这是持有集合视图的表视图的代码,

 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 个答案:

答案 0 :(得分:0)

您可能需要

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
}