UITableView滚动导致计时器停止

时间:2015-10-06 15:12:08

标签: ios swift uitableview

我刚开始参加ios应用程序开发的在线课程,这是我在SO上的第一篇文章,所以请耐心等待。

进一步进行秒表练习,我设法将圈数记录到一个阵列中,后者填充主故事板上的 UITableView

一切都很好,除了一件事,当我开始滚动桌子时,秒表因某些我不知道的原因而停止,我希望你的答案可以被初学者理解。

我正在使用 XCode 7 。感谢。

正如您可能看到的,代码看起来很难看,正如我所说,这些是我编写代码的第一行,所以我认为如果我发布它会更好。

总结一下,主故事板包含两个计数器的两个标签,主计数器(计时器)一直继续,直到按下复位按钮但是lapTimer设置为零,其值保存到lapArr表,之后计数器再次开始。这是所有代码:

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

var timer = NSTimer()
var lapTimer = NSTimer()
var time = 0
var sec = 0
var min = 0
var lapTime = 0
var lapSec = 0
var lapMin = 0
var lap = ""
var lapArr = [""]


@IBOutlet var lapTableView: UITableView!

@IBOutlet var lapCentiSecLab: UILabel!
@IBOutlet var lapSecLab: UILabel!
@IBOutlet var lapMinLab: UILabel!

@IBOutlet var mainCentiSecLab: UILabel!
@IBOutlet var mainSecLab: UILabel!
@IBOutlet var mainMinLab: UILabel!

func updateTime() {

    time++
    if time <= 9 {
        mainCentiSecLab.text = "0\(time)"
    } else {
        mainCentiSecLab.text = "\(time)"
    }
    if time == 99 {
        time = 0
        sec++
        if sec <= 9 {
            mainSecLab.text = "0\(sec)"
        } else {
            mainSecLab.text = "\(sec)"
        }
    }
    if sec == 59 {
        sec = 0
        min++
        if min <= 9 {
            mainMinLab.text = "0\(min)"
        } else {
            mainMinLab.text = "\(min)"
        }
    }
}

func updateTimeLap () {

    lapTime++
    if lapTime <= 9 {
        lapCentiSecLab.text = "0\(lapTime)"
    } else {
        lapCentiSecLab.text = "\(lapTime)"
    }

    if lapTime == 99 {
        lapTime = 0
        lapSec++
        if lapSec <= 9 {
            lapSecLab.text = "0\(lapSec)"
        } else {
            lapSecLab.text = "\(lapSec)"
        }
    }

    if lapSec == 60 {
        lapSec = 0
        lapMin++
        if lapMin <= 9 {
            lapMinLab.text = "0\(lapMin)"
        } else {
            lapMinLab.text = "\(lapMin)"
        }
    }
}

func lapTimeFormatter() {

    if lapTime <= 9 {
        lapCentiSecLab.text = "0\(lapTime)"
    } else {
        lapCentiSecLab.text = "\(lapTime)"
    }

    if lapSec <= 9 {
        lapSecLab.text = "0\(lapSec)"
    } else {
        lapSecLab.text = "\(lapSec)"
    }

    if lapMin <= 9 {
        lapMinLab.text = "0\(lapMin)"
    } else {
        lapMinLab.text = "\(lapMin)"
    }

}

@IBAction func play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1/100, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    lapTimer = NSTimer.scheduledTimerWithTimeInterval(1/100, target: self, selector: Selector("updateTimeLap"), userInfo: nil, repeats: true)

}

@IBAction func pause(sender: AnyObject) {

    timer.invalidate()
    lapTimer.invalidate()

}



@IBAction func reset(sender: AnyObject) {

    timer.invalidate()
    lapTimer.invalidate()

    time = 0
    sec = 0
    min = 0

    lapTime = 0
    lapSec = 0
    lapMin = 0

    lapTimeFormatter()

    mainCentiSecLab.text = "0\(time)"
    mainSecLab.text = "0\(sec)"
    mainMinLab.text = "0\(min)"

    lapArr.removeAll()
    lapTableView.reloadData()

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return (lapArr.count)

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

{

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = lapArr[indexPath.row]

    return cell

}

@IBAction func lap(sender: AnyObject) {


    lapTimer.invalidate()

    lap = lapMinLab.text! + " : " + lapSecLab.text! + " . " + lapCentiSecLab.text!

    lapArr.append(lap)

    lapTime = 0
    lapSec = 0
    lapMin = 0

    lapTimeFormatter()

    lapTimer = NSTimer.scheduledTimerWithTimeInterval(1/100, target: self, selector: Selector("updateTimeLap"), userInfo: nil, repeats: true)

    lapTableView.reloadData()

}

override func viewDidLoad() {
    super.viewDidLoad()

    lapTableView.allowsSelection = false
    lapTimeFormatter()

    mainCentiSecLab.text = "0\(time)"
    mainSecLab.text = "0\(sec)"
    mainMinLab.text = "0\(min)"

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

0 个答案:

没有答案