Ios CollectionView位于另一个CollectionViewCell内

Ios CollectionView位于另一个CollectionViewCell内,ios,uicollectionview,swift3,Ios,Uicollectionview,Swift3,我正在尝试在另一个collectionViewCell中创建collectionView。我可以获取外部collectionView的单元格,但是编译器没有执行内部单元格。我怎样才能得到这两个细胞 另外,我在这方面是新手 CollectionViewController类: class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{ @IBOutlet var c

我正在尝试在另一个
collectionViewCell
中创建
collectionView
。我可以获取外部
collectionView
的单元格,但是编译器没有执行内部单元格。我怎样才能得到这两个细胞

另外,我在这方面是新手

CollectionViewController类:

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

@IBOutlet var collectionView1: UICollectionView!

var coll2 = CollectionViewCell()
override func viewDidLoad() {
    super.viewDidLoad()
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

   return 3
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 150)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell
    cell.backgroundColor = UIColor.black

    return cell

}

}
class CollectionViewCell: UICollectionViewCell  {

@IBOutlet var collectionView2: UICollectionView!

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
 }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView2.frame.width, height: 150)
}

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath)as! CollectionViewCell2

    cell1.backgroundColor = UIColor.red

    return cell1

 }
CollectionViewCell类:

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

@IBOutlet var collectionView1: UICollectionView!

var coll2 = CollectionViewCell()
override func viewDidLoad() {
    super.viewDidLoad()
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

   return 3
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 150)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell
    cell.backgroundColor = UIColor.black

    return cell

}

}
class CollectionViewCell: UICollectionViewCell  {

@IBOutlet var collectionView2: UICollectionView!

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
 }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView2.frame.width, height: 150)
}

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath)as! CollectionViewCell2

    cell1.backgroundColor = UIColor.red

    return cell1

 }

}

将内部集合视图的委托和数据源设置为第一个集合视图单元格

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

    self.collectionView2?.delegate = self
    self.collectionView2?.dataSource = self
}

CollectionViewCell
没有设置
collectionView2
的原因是您没有将
delegate
dataSource
collectionView2
属性设置到
CollectionViewCell
的实例

我在
CollectionViewCell
中创建了一个名为
setupview
的方法,以便在
CollectionViewCell
中退出
CollectionViewCell
队列时调用该方法,以便正确设置委托和数据源

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

  override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
  }


  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 150)
  }

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell

    cell.backgroundColor = UIColor.black
    cell.setupViews() // added this call

    return cell
  }

}


我已尝试将其放入CollectionViewCell(外部单元格)中,但出现错误:在展开可选值try self.collectionView2?时意外发现零。委托=自我。collectionView2?数据源=自我