ios - CollectionView 在另一个 CollectionViewCell 中

标签 ios uicollectionview swift3

我正在尝试在另一个 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

最佳答案

将内部 Collection View 的委托(delegate)和数据源设置为第一个 Collection View 单元格。

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

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

关于ios - CollectionView 在另一个 CollectionViewCell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868303/

相关文章:

ios - 类型 'Any' 在 xcode 8 Swift 3 中没有下标成员

swift - 在 Swift 3 中将 C 数组 ptr 转换为原生数组

ios - 在 iPhone 上播放直播视频

ios Mantle - 使用字典属性覆盖默认初始化

ios - UITableViewHeaderFooterView 和界面生成器 : how to adjust width for different devices?

swift - 如何使用分页设置水平 UICollectionView,但每页的项目从左到右和从上到下排列

ios - CollectionViewCells 随滚动变化

objective-c - 创建 UIImage 抠图

ios - UICollectionView 的 didSelectItemAtIndexPath 方法只被调用一次

ios - UINavigationController 不是全屏(swift)