1

这是我的视图控制器,我在其中设置了我的 collectionView 及其方法。

class HomeScreenViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


@IBOutlet weak var collectionViewOne: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
     collectionViewOne.register(UINib(nibName: "recomendedcell1", bundle: nil), forCellWithReuseIdentifier: "cell1")
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
    return cell
}

这是我的自定义单元格文件

class CustomCellOneCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

我还使用下面的代码进行单元格声明。

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

我检查了所有连接,仔细检查了标识符的名称。我的视图控制器是标签栏控制器的一部分。

4

4 回答 4

3

虽然注册 nib,nibName 应该是 Cell namee 的意思是 CustomCellOneCollectionViewCell

collectionViewOne.register(UINib(nibName: "CustomCellOneCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell1")
collectionViewOne.delegate = self
collectionViewOne.datasource = self

并在 cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell
    return cell
}
于 2018-05-24T11:24:42.137 回答
0

确保您的单元格 recommendedcell1 确保其超类是 CustomCellOneCollectionViewCell

在此处输入图像描述

为什么这条线

let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell

只是dequeueReusableCell

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

所以你的细胞将是

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CustomCellOneCollectionViewCell
    return cell
}
于 2018-05-24T11:16:40.843 回答
0
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! CustomCellOneCollectionViewCell

    return cell
}
于 2018-05-24T11:18:35.440 回答
0

删除此行

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = Bundle.main.loadNibNamed("recommendedcell1", owner: self, options: nil)?.first as! CustomCellOneCollectionViewCell
        return cell
}

试试这个

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

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

}
于 2018-05-24T11:22:29.540 回答