From e0c0e24188b17be61291215fb3f476f0f974109e Mon Sep 17 00:00:00 2001 From: Dallon Archibald Date: Sat, 23 Jan 2021 19:18:50 -0500 Subject: [PATCH] Configured Basic Functionalities and Updated Table Functions --- ProjectClock/StopwatchViewController.swift | 47 +++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/ProjectClock/StopwatchViewController.swift b/ProjectClock/StopwatchViewController.swift index a930114..ca284c4 100644 --- a/ProjectClock/StopwatchViewController.swift +++ b/ProjectClock/StopwatchViewController.swift @@ -37,17 +37,37 @@ class StopwatchViewController: UIViewController { timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(count), userInfo: nil, repeats: true) } - - @IBAction func stop(_ sender: UIButton) { - } - @IBAction func reset(_ sender: UIButton) { - } - @IBAction func lap(_ sender: UIButton) { - } - @objc fileprivate func count() { seconds += 1 - print(seconds) + if seconds == 60 { + minutes += 1 + seconds = 0 + } + if minutes == 60 { + hours += 1 + minutes = 0 + } + if hours == 24 { + timer.invalidate() + } + secondLabel.text = "\(seconds)" //if time, make it display 01, 02... + minuteLabel.text = minutes == 0 ? "00" : "\(minutes)" + hourLabel.text = hours == 0 ? "00" : "\(hours)" + } + + @IBAction func stop(_ sender: UIButton) { + timer.invalidate() + } + + @IBAction func reset(_ sender: UIButton) { + } + + @IBAction func lap(_ sender: UIButton) { + let currentTime = "\(hours):\(minutes):\(seconds)" + lappedTimes.append(currentTime) + + let indexPath = IndexPath(row: lappedTimes.count - 1, section: 0) + tableView.insertRows(at: [indexPath], with: .automatic) } } @@ -60,6 +80,15 @@ extension StopwatchViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "lapCell", for: indexPath) cell.textLabel?.text = lappedTimes[indexPath.row] + cell.selectionStyle = .none return cell } + + func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + if editingStyle == .delete { + lappedTimes.remove(at: indexPath.row) + + tableView.deleteRows(at: [indexPath], with: .automatic) + } + } }