Reputation: 3823
I am trying to update a UILabel that exists on my UICollectionView Cell. Although I am having the problem where only the first cell gets updated with the new string
I firstly tried to hook up an IBOutlet to the UILabel in the Cell, however got this problem:
Main.storyboard: error: Illegal Configuration: Connection "name" cannot have a prototype object as its destination.
Next, i tried to use a tag - however using this method, only one of the UICollectionView Cells get updated.
// 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
}
How can i make it so that all of the cells are updated? I the future this label will change so it needs to be dynamic
I used to achieve this in Objective-C using:
UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
Upvotes: 2
Views: 2376
Reputation: 92409
The Swift counterpart for the Objective-C declaration:
UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
is:
let label = cell.contentView.viewWithTag(LABEL_TAG) as UILabel
So replace:
var nameLbl : UILabel? = self.collectionView.viewWithTag(100) as? UILabel;
with:
let nameLbl = cell.contentView.viewWithTag(100) as UILabel
//or
//let nameLbl = cell.viewWithTag(100) as UILabel
You will then be able to write:
nameLbl.text = "woof woof"
Upvotes: 2