【问题标题】:UICollectionViewController can't reach cellUICollectionViewController 无法到达单元格
【发布时间】:2017-10-22 17:54:50
【问题描述】:

我试图更改 UICollectionViewCell 中标签的文本。 ViewCell 类链接到单元格,设置了出口,并在 viewController 中注册了

// Register cell classes
    self.collectionView!.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

在 cellForItemAt...

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell

但我无法访问该标签。 imageView 也有同样的问题。如果我尝试将文本设置为:

cell.hund.text = "bla"

标签“hund”是已知的,但它抛出标签为 nil。

谢谢汤姆

ImageCollectionVC

import UIKit
private let reuseIdentifier = "cell"

class ImageCollectionVC: UICollectionViewController {

var listOfImages = [Int: KKImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView!.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    listOfImages = xmlParseImageSource()
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

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

    let imgUrl = listOfImages[indexPath.row]?.imagePreviewUrl

    do {
        let bindData = try Data(contentsOf: imgUrl!)
        let img = UIImage(data: bindData)

        cell.hund.text = "bla"

        //cell.img.image = img
        //cell.img.image = UIImage(data: bindData)
    } catch {
        print(error.localizedDescription)
    }
    cell.backgroundColor = UIColor.blue
    return cell
}
}

ImageCollectionViewCell

import UIKit

class ImageCollectionViewCell: UICollectionViewCell {
 @IBOutlet weak var img: UIImageView!
 @IBOutlet weak var hund: UILabel!  
}

【问题讨论】:

  • 请分享完整的 viewController 代码,其中包含 collectionView 和 ImageCollectionViewCell 代码以检查问题所在。
  • 如果有,您是否有 UICollectionViewCell 的 xib,请确保您已正确连接插座。
  • @rameez 不,我没有 xib。
  • 检查我的答案

标签: ios xcode swift3 uicollectionview uicollectionviewcell


【解决方案1】:

Thomas 从 nib 中醒来只需初始化您的 UIImageView 和 UILabel 并将它们添加到单元格中,它应该可以工作 :)

 override func awakeFromNib() {
   super.awakeFromNib()
   // Initialization code, you obviously need to set the frames/constraints as per your requirement

   img = UIImageView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
   hund = UILabel(frame: CGRect(x: 50, y: 0, width: 300, height: 40))
   self.addSubview(img)
   self.addSubview(hund)
}

如果您使用的是 xib,请不要注册该类,而是使用 nib 注册,它应该是:

self.collectionView.register(UINib.init(nibName: "ImageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier )

【讨论】:

  • 感谢 rameez,但我该如何初始化它们呢?在界面生成器中?抱歉,我对 swift 很陌生,我 1200 页的 swift 书没有告诉我如何,我分别找不到:)
  • 它不起作用。顺便说一句,我不明白为什么我应该创建一个新标签。我会在界面生成器中添加它。单元格的子类设置为 ImageCollectionViewCell。我将在一个新项目中尝试此方法以检查它是否有效。
  • 你说你的单元格没有xib。
  • 好的,如果您有 xib,请确保您已正确设置插座,在这种情况下您不需要初始化标签和图像。
  • 对不起,我是德国人,我必须为我翻译你的句子。现在我知道xib是什么了。就像你在我的问题中看到的那样,网点已经设置好了。
猜你喜欢
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多