Khoury
Khoury

Reputation: 431

Which UICollectionView function should be used to update UILabels within a cell?

I want to update a UILabel within a UICollectionViewCell every second, which UICollectionView function should I be using for this?

The UILabel is coded in this way.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
cell.dayCountLabel.text = dayCount(Globals.datesArray[indexPath.item])
}

And receives data in this way.

func dayCount (dayCount: String) -> String{
    let day = dayCount
    let date2 = NSDate()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
    let date1: NSDate = dateFormatter.dateFromString(day)!
    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day], fromDate: date1, toDate: date2, options: NSCalendarOptions.init(rawValue: 0))
    let difference = String("\(abs(diffDateComponents.day))")
    return difference
}

Sorry for the rookie question.

Upvotes: 3

Views: 101

Answers (1)

Evgeny Karkan
Evgeny Karkan

Reputation: 9612

In case that you have prepared data source text to display in UILabel, call reloadData function:

yourCollectionView.reloadData()

Otherwise, you will get the old text value.

Update

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("reuseId", forIndexPath: indexPath) as! UICollectionViewCell

    //update cell subviews here 
    return cell
}

In your case you should create and return UICollectionViewCell custom subclass, where you implemented dayCountLabel, don't know your custom class name...

Upvotes: 2

Related Questions