【问题标题】:CollectionView inside another CollectionViewCell另一个 CollectionViewCell 中的 CollectionView
【发布时间】:2016-10-05 07:52:58
【问题描述】:

我正在尝试在另一个 collectionViewCell 中创建 collectionView。我可以得到外部collectionView 的单元格,但是编译器没有执行内部单元格。如何获得两个单元格?

P.S 我是新手。

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

}

}

CollectionViewCell 类:

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

 }

enter image description here }

【问题讨论】:

    标签: ios uicollectionview swift3


    【解决方案1】:

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

    override func awakeFromNib() 
    {
        super.awakeFromNib()
        // Initialization code
    
        self.collectionView2?.delegate = self
        self.collectionView2?.dataSource = self
    }
    

    【讨论】:

    • 我尝试将它放入 CollectionViewCell(外部单元格)中,但出现错误:在展开可选值时意外发现 nil
    • 试试 self.collectionView2?.delegate = self self.collectionView2?.dataSource = self
    【解决方案2】:

    CollectionViewCell之所以没有设置collectionView2是因为你没有将collectionView2delegatedataSource属性设置为CollectionViewCell的实例。

    我在CollectionViewCell 中创建了一个名为setupViews 的方法,以便在您将CollectionViewController 中的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
      }
    
    }
    

    class CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
      @IBOutlet weak var collectionView2: UICollectionView!
    
      // added this method
      func setupViews() {
        collectionView2.delegate = self
        collectionView2.dataSource = self
      }
    
      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) 
    
        cell1.backgroundColor = UIColor.red
    
        return cell1
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多