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

时间:2016-06-08 08:30:11

标签: ios swift

我在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
}

1 个答案:

答案 0 :(得分:0)

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

使用此代码