Ios 应使用哪个UICollectionView函数更新单元格内的UILabel?
我想每秒更新UICollectionViewCell中的UILabel,我应该使用哪个UICollectionView函数进行此更新 UILabel是这样编码的Ios 应使用哪个UICollectionView函数更新单元格内的UILabel?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我想每秒更新UICollectionViewCell中的UILabel,我应该使用哪个UICollectionView函数进行此更新 UILabel是这样编码的 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { cell.dayCountLabel.text = dayCount(Glob
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
}
并以这种方式接收数据
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
custom,您在其中实现了dayCountLabel
,不知道自定义类名…可能是因为hour应该重新配置逻辑,而不是每秒钟更新一次?我的UICollectionViewCell有一个显示秒数的倒计时计时器,这就是为什么它必须每秒钟更新一次,所以请将逻辑放在自定义集合视图单元格类中,并每秒钟调用一次方法。我没有预定义的方法,但它不会更新UILabel,只更新集合视图一切都应该工作,因为调用reloadData
调用cellForItemAtIndexPath
函数,您可以将新日期设置为UILabel
…顺便说一句func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{cell.dayCountLabel.text=dayCount(Globals.datesArray[indexPath.item]))
看起来很奇怪,你应该在这里创建并返回单元格你说的创建和返回单元格是什么意思?请注意,创建NSDateFormatter
实例是一个耗时的操作,这就是为什么我猜你的UI性能很慢的原因。找时间检查一下使用它的良好实践