簡體   English   中英

如何在UICollectionViewCell上使用實時計時器?

[英]How to use live timer on UICollectionViewCell?

我在UICollectionView單元上實現了一個倒數計時器,並從Web服務獲取時間,但是當我返回並再次進入UICollectionView時,它顯示的是靜態時間,類時間根據剩余的Web服務時間而減少。

但是我想讓它每減少一秒鍾就會實時顯示,感謝您的幫助。

我的代碼:

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)"

使用此代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM