gpt4 book ai didi

ios - 如何在 UICollectionViewCell 上使用实时计时器?

转载 作者:行者123 更新时间:2023-11-30 13:21:52 25 4
gpt4 key购买 nike

我在UICollectionView中的单元格上实现了一个倒计时器,并从网络服务中获取时间,但当我返回并再次出现在UICollectionView类中时,它显示静态时间时间根据剩余的网络服务时间而减少。

但我想在每次减少一秒时实时显示它,感谢任何帮助。

我的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ManageObjectCollectionViewCell
revealTime = getRevealtime[indexPath.section].objectAtIndex(indexPath.row) as! Int
minutes = revealTime / 60
seconds = revealTime % 60
cell.HourLabel.text = "0"
cell.MinLabel.text = NSString(string: "\(minutes)" ) as String
cell.sec.text = NSString(string: "\(seconds)" ) as String
}

最佳答案

    let currentTime = NSDate.timeIntervalSinceReferenceDate()

//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = currentTime - startTime

//calculate the hours in elapsed time.
let hours = UInt8(elapsedTime / 3600.0)
elapsedTime -= (NSTimeInterval(hours) * 3600)


//calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)

//calculate the seconds in elapsed time.
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)

//find out the fraction of milliseconds to be displayed.
// let fraction = UInt8(elapsedTime * 100)

//add the leading zero for minutes, seconds and millseconds and store them as string constants

let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let hourss = String(format: "%02d", hours)

//concatenate minuets, seconds and milliseconds as assign it to the UILabel
lblDisplayTime.text = "\(hourss):\(strMinutes):\(strSeconds)"

使用此代码

关于ios - 如何在 UICollectionViewCell 上使用实时计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com