【发布时间】: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