【发布时间】:2016-05-17 21:19:37
【问题描述】:
我有一个自定义 UICollectionViewCell,我试图将值从我的视图控制器传递给它。我可以将图像传递给单元格,但初始化时其他任何内容都为零。
View Controller中的相关代码:
override func viewDidLoad() {
self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "Cell")
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCustomCell
cell.someValue = 5
cell.imageView.image = UIImage(named: "placeholder.png")
return cell
}
在自定义单元格类中:
var someValue: Int!
var imageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView(frame: CGRectMake(0, 0, frame.width, frame.height))
contentView.addSubview(imageView)
let someValueLabel = UILabel()
someValueLabel.frame = CGRectMake(0, 75, 200, 30)
someValueLabel.text = "Value is: \(someValue)"
self.addSubview(someValueLabel)
}
图像从 UICollectionView 成功传递,我可以显示它,但“someValue”始终为零。
我做错了什么?
【问题讨论】:
-
另外,您不应该将您的 UIImageView 属性称为“imageView”。由于 UITableViewCell 已经包含一个使用这个名称的。它可能会导致不良影响。也许它已经修复了,但我已经遇到了一些麻烦。喜欢这个Q:stackoverflow.com/questions/11681273/…
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewdelegate