我正在尝试更新我的UICollectionView Cell上存在的UILabel。虽然我遇到的问题是只有第一个单元格使用新字符串
进行更新我首先尝试将IBOutlet连接到Cell中的UILabel,但遇到了这个问题:
Main.storyboard: error: Illegal Configuration: Connection "name" cannot have a prototype object as its destination.
接下来,我尝试使用标记 - 但是使用此方法,只有一个UICollectionView单元格得到更新。
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 1000
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell
var nameLbl : UILabel? = self.collectionView.viewWithTag(100) as? UILabel;
nameLbl?.text = "woof woof"
return cell
}
我怎样才能让所有细胞都更新?我未来这个标签会改变,所以它需要是动态的
我曾经在Objective-C中使用以下方法实现这一目标:
UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
答案 0 :(得分:2)
Objective-C声明的Swift副本:
UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
是:
let label = cell.contentView.viewWithTag(LABEL_TAG) as UILabel
所以替换:
var nameLbl : UILabel? = self.collectionView.viewWithTag(100) as? UILabel;
使用:
let nameLbl = cell.contentView.viewWithTag(100) as UILabel
//or
//let nameLbl = cell.viewWithTag(100) as UILabel
然后您就可以写下:
nameLbl.text = "woof woof"