应使用哪个UICollectionView函数来更新单元格中的UILabels



我想每秒更新UICollectionViewCell中的UILabel,我应该使用哪个UICollectionView函数?

UILabel就是这样编码的。

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

并以这种方式接收数据。

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
}

抱歉问了新手的问题。

如果您已经准备好要在UILabel中显示的数据源文本,请调用reloadData函数:

yourCollectionView.reloadData()

否则,您将获得旧的文本值。

更新

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

在您的情况下,您应该创建并返回UICollectionViewCell自定义子类,在那里您实现了dayCountLabel,但不知道您的自定义类名。。。

最新更新