I have just started taking an online course for ios app development and this is my first post on SO, so bear with me guys.
Taking further a stopwatch exercise, I managed to log laps into an array and the latter fills up a UITableView on the main storyboard.
Everything works fine except one thing, when I start scrolling the table, the stopwatch stops for some reason that I don't know and I hope that your answers will be understandable by a beginner.
I'm using XCode 7. Thanks.
As you might see, the code looks ugly and as I said these are my first lines in coding so I thought it would be better if I post it all.
To Summarise, the main story board contains two label for two counters, the main counter (timer) continue until reset button is pressed but the lapTimer is set to zero and its value is saved to the lapArr table, after which the counter starts again. here is all the code :
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.
}
}