【问题标题】:Issues while showing content in collection view inside table view in swift快速在表格视图内的集合视图中显示内容时出现问题
【发布时间】:2019-07-24 01:34:27
【问题描述】:

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

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

数据不是以水平方式进入的,它复制了集合视图的单元格。这是我的 Collection 视图代码,用于在其中填充数据。

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 中显示我的数据,就像我问题中的第一张图片一样。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    你可能需要

    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
    }
    

    【讨论】:

    • 它在这个委托上崩溃,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" 返回 headerView }。此行出现索引超出范围错误 headerView.categoryNameLbl.text = categoryArray[section].catName。 @Sh_Khan
    • 更改numberOfRowsInSection ??
    • 是的,我按照您在回答中提到的那样放置了代码,但在 viewForHeaderInSection 委托中崩溃了。 @Sh_Khan
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多