Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 包含collectionview的原型单元格的TableView不返回所有TableViewCell_Ios_Swift_Xcode_Uitableview_Uicollectionview - Fatal编程技术网

Ios 包含collectionview的原型单元格的TableView不返回所有TableViewCell

Ios 包含collectionview的原型单元格的TableView不返回所有TableViewCell,ios,swift,xcode,uitableview,uicollectionview,Ios,Swift,Xcode,Uitableview,Uicollectionview,所以我有一个TableViewController,它有一个tableview,当前有4个原型单元。每个原型单元中都有一个collectionview,该collectionview包含collectionviewcells。我试图让tableview显示单独的tableview单元格,其中包含collectionview,但到目前为止,我的代码只返回第一个tableview单元格,而没有返回其余单元格。这是我第一次尝试在tableview中使用collectionview,所以我确信这是我做错

所以我有一个TableViewController,它有一个tableview,当前有4个原型单元。每个原型单元中都有一个collectionview,该collectionview包含collectionviewcells。我试图让tableview显示单独的tableview单元格,其中包含collectionview,但到目前为止,我的代码只返回第一个tableview单元格,而没有返回其余单元格。这是我第一次尝试在tableview中使用collectionview,所以我确信这是我做错的小事情。这是我的密码:

import UIKit

class AlbumSongArtistTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         if section == 0 {
                   return 1
               } else if section == 1 {
                   return 1
               } else if section == 2 {
                   return 1
               } else {
               return 1
            }
        }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "GenresTableViewCell", for: indexPath) as! GenresTableViewCell


            return cell
        } else if indexPath.section == 1 {

            let cell = tableView.dequeueReusableCell(withIdentifier: "FeaturedArtistsCollectionViewTableViewCell", for: indexPath) as! FeaturedArtistsCollectionViewTableViewCell

            return cell
        } else if indexPath.section == 2 {

            let cell = tableView.dequeueReusableCell(withIdentifier: "NewReleasesCollectionViewTableViewCell", for: indexPath) as! NewReleasesCollectionViewTableViewCell

            return cell
        } else {

            let cell = tableView.dequeueReusableCell(withIdentifier: "SomthingElseCollectionViewTableViewCell", for: indexPath) as! SomthingElseCollectionViewTableViewCell

            return cell
        }
    }

    //MARK:- collection view delegate

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
                          return 10
                      } else if section == 1 {
                          return 8
                      } else if section == 2 {
                          return 6
                      } else {
                      return 4
                   }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.section == 0{

            let cell:GenresInsideTableViewCellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "GenresInsideTableViewCellCollectionViewCell", for: indexPath) as! GenresInsideTableViewCellCollectionViewCell

            return cell
        } else if indexPath.section == 1 {

        let cell:FeaturedArtistsInsideTableViewCellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedArtistsInsideTableViewCellCollectionViewCell", for: indexPath) as! FeaturedArtistsInsideTableViewCellCollectionViewCell

        return cell
        } else if indexPath.section == 2 {
            let cell:NewReleasesInsideTableViewCellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewReleasesInsideTableViewCellCollectionViewCell", for: indexPath) as! NewReleasesInsideTableViewCellCollectionViewCell

            return cell
        } else {
            let cell:SomethingElseInsideTableViewCellCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SomethingElseInsideTableViewCellCollectionViewCell", for: indexPath) as! SomethingElseInsideTableViewCellCollectionViewCell

            return cell
        }
    }
}
以下是我的故事板的外观:

下面是我运行模拟器时的样子:

更新:以下是我的tableviewcell子类的代码:

import UIKit

class FeaturedArtistsCollectionViewTableViewCell: UITableViewCell {

    @IBOutlet weak var faCollectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension FeaturedArtistsCollectionViewTableViewCell {

    func setCollectionViewDataSourceDelegate
        <D: UICollectionViewDelegate & UICollectionViewDataSource>
        (_ dataSourceDelegate: D, forRow row:Int)
    {
        faCollectionView.delegate = dataSourceDelegate
        faCollectionView.dataSource = dataSourceDelegate

        faCollectionView.reloadData()
    }
}
导入UIKit
类功能DartIstCollectionViewTableViewCell:UITableViewCell{
@IBOutlet弱var faCollectionView:UICollectionView!
重写func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}
覆盖功能设置选定(uSelected:Bool,动画:Bool){
super.setSelected(选定,动画:动画)
//为所选状态配置视图
}
}
扩展功能DartIstCollectionViewTableViewCell{
func setCollectionViewDataSourceDelegate
(uu数据源elegate:D,forRow:Int)
{
faCollectionView.delegate=dataSourceDelegate
faCollectionView.dataSource=dataSourceDelegate
faCollectionView.reloadData()
}
}

您是否为表视图实现了
numberOfSections
?如果。它只有一个节。@Paulw11,所以我实现了这个代码
覆盖func numberOfSections(在tableView:UITableView中)->Int{return 4}
,但是当我运行它时,它会抛出一个错误。错误是
无法将标识符为
的UICollectionElementKindCell类型的视图出列,这让我很困惑,因为我的所有CollectionView都显示它们在我的故事板上连接到各自的collectionviewcell子类CollectionView数据源方法中的节号始终为0,因为它是集合视图部分,而不是表视图部分。表视图单元格应该是集合视图单元格数据源,而不是视图控制器。然后可以将适当的数据分配给表cell@Paulw11对不起,伙计,我不太明白你的意思。我已经更新了上面的问题,向您展示了我的一个tableviewcell子类的代码,您可以看到collecionview数据源和委托都设置在那里。在您的
cellForItemAt
中,您正在根据节号做出决定,但节号始终为0,因为每个集合视图中只有一个节,每个单元格中有不同的集合视图。