Ios 在swift表视图内的集合视图中显示内容时出现的问题

Ios 在swift表视图内的集合视图中显示内容时出现的问题,ios,swift,Ios,Swift,我在表视图中使用了集合视图,并在集合视图中填充来自后端的数据。现在,当我重新加载数据时,它不会以我试图显示的方式显示UI,我希望我的UI看起来像这样, 但当我运行我的应用程序并打开VC时,我的表视图是这样的, 数据不是以水平方式来的,它复制了集合视图的单元格。这是我的集合视图代码,用于填充其中的数据 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

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

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

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

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

您可能需要

一,-

二,-

三,-

你可能需要

一,-

二,-

三,-


它正在这个委托上崩溃,func tableView(u tableView:UITableView,viewForHeaderInSection:Int)->UIView?{let headerView=Bundle.main.loadNibNamed(“KnowledgeHeaderTVC”,所有者:self,选项:nil)?。首先是!KnowledgeHeaderTVC headerView.categoryNameLbl.text=categoryArray[section]。catName headerView.articlesLbl.text=“(categoryArray[section].blogArray.count)”+“articles”返回headerView}。此行出现索引超出范围错误headerView.categoryNameLbl.text=categoryArray[section].catName@Sh_khanched
段落中的行数
?是的,正如您在回答中提到的,我放置了代码,但在标题段落代表的视图中出现崩溃@Sh_KhanIt正在崩溃此委托func tableView(_tableView:UITableView,viewForHeaderInSection:Int)->UIView?{let headerView=Bundle.main.loadNibNamed(“KnowledgeHeaderTVC”,所有者:self,选项:nil)?。首先是!KnowledgeHeaderTVC headerView.categoryNameLbl.text=categoryArray[section]。catName headerView.articlesLbl.text=“(categoryArray[section].blogArray.count)”+“articles”返回headerView}。此行出现索引超出范围错误headerView.categoryNameLbl.text=categoryArray[section].catName@Sh_khanched
段落中的行数
?是的,正如您在回答中提到的,我放置了代码,但在标题段落代表的视图中出现崩溃@施胡汗
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 1 // change this to 1
}
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
}
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
}