Ios UITableView中包含多个UICollectionView,其中包含部分-swift

Ios UITableView中包含多个UICollectionView,其中包含部分-swift,ios,uitableview,swift,uicollectionview,Ios,Uitableview,Swift,Uicollectionview,基本上,我试图获得与此类似的结果: 然而,我想要两个集合视图,第一部分一个,第二部分另一个 我如何做到这一点? 我在这里猛击我的头。。。每次我尝试设置委托和数据源时,它都会失败,并说我需要注册一个nib 我也不知道如何在它的cellforrowatinexpath中分离集合视图,如何才能看到哪一个正在加载? 我如何才能让它正确加载,以便第一部分有self.genre,第二部分有self.radioStat 这是我第二次尝试设置委托和数据源时遇到的错误 **由于未捕获的异常“NSInternalI

基本上,我试图获得与此类似的结果:

然而,我想要两个集合视图,第一部分一个,第二部分另一个

我如何做到这一点? 我在这里猛击我的头。。。每次我尝试设置委托和数据源时,它都会失败,并说我需要注册一个nib 我也不知道如何在它的cellforrowatinexpath中分离集合视图,如何才能看到哪一个正在加载? 我如何才能让它正确加载,以便第一部分有self.genre,第二部分有self.radioStat

这是我第二次尝试设置委托和数据源时遇到的错误

**由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符CollectionViewCell34的UICollectionElementKindCell类型的视图出列-必须为标识符注册nib或类,或连接情节提要中的原型单元” **

tableViewCellCollectionView.swift

import Foundation
import UIKit
let collectionViewCellIdentifier: NSString = "CollectionViewCell"
class tableViewCellCollectionView: UITableViewCell  {

var collectionView: UICollectionView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
    layout.minimumLineSpacing = 5
    layout.itemSize = CGSizeMake(91, 91)
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: "CollectionViewCell")
    self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: collectionViewCellIdentifier)
    self.collectionView.backgroundColor = UIColor.lightGrayColor()
    self.collectionView.showsHorizontalScrollIndicator = false
    self.contentView.addSubview(self.collectionView)
    self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)

}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    let frame = self.contentView.bounds
    self.collectionView.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
}

func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
    self.collectionView.dataSource = delegate
    self.collectionView.delegate = delegate
    self.collectionView.tag = index
    self.collectionView.reloadData()
}
}
import Foundation
import UIKit
let collectionViewCellIdentifier34: NSString = "CollectionViewCell34"
class tableViewCellCollectionView2: UITableViewCell  {
var collectionView2: UICollectionView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)


    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
    layout.minimumLineSpacing = 1
    layout.itemSize = CGSizeMake(86, 54)
    layout.scrollDirection = UICollectionViewScrollDirection.Vertical
    self.collectionView2 = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
    self.collectionView2.backgroundColor = UIColor.lightGrayColor()
    self.collectionView2.showsHorizontalScrollIndicator = false
    self.contentView.addSubview(self.collectionView2)
    self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)


}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    let frame = self.contentView.bounds
    self.collectionView2.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
}

func setCollectionViewDataSourceDelegate2(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
    self.collectionView2.dataSource = delegate
    self.collectionView2.delegate = delegate
    self.collectionView2.tag = index
    self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
    self.collectionView2.reloadData()
} 

}
   extension Player: UICollectionViewDataSource,UICollectionViewDelegate {


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.radioStat.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
        var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
        cell.title.text = rowData as String

        let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
        let userPost: String  = self.radioStat.objectAtIndex(indexPath.row) as String
        cell2.pinImage.image = UIImage(named: userPost)




    let collectionViewArray = self.sourceArray[collectionView.tag] as NSArray



    return cell
}
extension Player {


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell


    if (indexPath.section == 0){
        var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
        return cell2
    }else if (indexPath.section == 1){
        let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
        return cell3
    }
    return cell

}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.section == 0){
    if let collectionCell: tableViewCellCollectionView = cell as? tableViewCellCollectionView{
    collectionCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: indexPath.row)
    }
    }
    if (indexPath.section == 1){
    if let collectionCell2: tableViewCellCollectionView2 = cell as? tableViewCellCollectionView2{
        collectionCell2.setCollectionViewDataSourceDelegate2(dataSourceDelegate: self, index: indexPath.row)
    }
    }


    }


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if (indexPath.section == 0){
        return 50
    }

    if (indexPath.section == 1){
        return 450
    }else {
        return 100
    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return sourceArray.count
        }
}
您需要知道您的
collectionView
与哪个(tableView)部分相关。一种方法是将UICollectionView子类化并向其添加一个
tvSection
属性,就像Ash Furrow使用
AFIndexedCollectionView
添加一个
索引
属性一样

但是您似乎使用collectionView
标记来代替他的
索引
,大概是为了避免子类化。如果是这样,一个类似的闪避就是使用tableViewCell的
contentView
标记来指示单元格所在的区域。修改tableView
cellForRowAtIndexPath
方法以执行此操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell

    if (indexPath.section == 0){
        var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
        cell2.contentView.tag = indexPath.section
        return cell2
    } else if (indexPath.section == 1){
        let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
        cell3.contentView.tag = indexPath.section
        return cell3
    }
    cell.contentView.tag = indexPath.section
    return cell
}
由于collectionView是作为
contentView
的子视图添加的,因此可以使用
superview.tag
确定给定collectionView的(tableView)部分。因此,修改collectionView的
cellForItemAtIndexPath
以测试这一点:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let section = collectionView.superview.tag
    if section == 0 {
        let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
        // Now configure from the data...
        var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
        cell.title.text = rowData as String
        return cell
    else {
        let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
        // Now configure from the data...
        let userPost: String  = self.radioStat.objectAtIndex(indexPath.row) as String
        cell2.pinImage.image = UIImage(named: userPost)
        return cell2
    }
}

这需要一些打磨;特别是,我不确定您希望如何将数据(
sourceArray
genre
radioStat
)映射到tableView行和collectionView项。(我根据您现有的代码进行了猜测)。但这应该会给您一些可以使用的东西。

标记collectionView及其superview(tableViewCell.contentView):DGenius真是太棒了。奇妙的解决方案。