第一天:Swift秒表加UITableView记录展示
参考链接:https://www.jianshu.com/p/5caa93d66d02
(默背下代码添加了一个 tableView,重新手打一次,由于时间限制不再进行文字描述。)
这里主要记录一点,把定时器加到 runloop 中,模式为 commonModes:
1 RunLoop.current.add(self.time, forMode: .commonModes)
要不然滑动 tableView 的时候,定时器会停止,停止滑动的时候定时器又开始执行。
参考链接:http://blog.csdn.net/a359696929/article/details/51372539
https://www.cnblogs.com/kenshincui/p/6823841.html
1 import UIKit 2 3 class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var timeLable: UILabel! 6 @IBOutlet weak var leftButton: UIButton! 7 @IBOutlet weak var rightButton: UIButton! 8 @IBOutlet weak var tableView: UITableView! 9 10 var time: Timer! 11 var stringArr: [String]! 12 var msInt: Int = 0 13 var recordCnt: Int = 0 14 var isStart: Bool = false 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 // Do any additional setup after loading the view, typically from a nib. 19 20 setupUI() 21 } 22 23 func numberOfSections(in tableView: UITableView) -> Int { 24 return 1 25 } 26 27 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 28 return stringArr.count 29 } 30 31 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 32 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 33 34 cell.textLabel?.text = String(format: "%d -- %@", indexPath.row + 1, stringArr[indexPath.row]) 35 cell.textLabel?.textColor = UIColor.white 36 37 return cell 38 } 39 40 @IBAction func leftButtonAction(_ sender: UIButton) { 41 if !isStart { 42 msInt = 0 43 44 updateTime() 45 stringArr.removeAll()
self.tableView.reloadData()
46 47 setButtonState(self.leftButton, isEnable: false) 48 self.leftButton.setTitle("记录", for: .normal) 49 self.rightButton.setTitle("开始", for: .normal) 50 } else { 51 stringArr.append(self.timeLable.text!) 52 self.tableView.reloadData() 53 } 54 } 55 56 @IBAction func rightButtonAction(_ sender: UIButton) { 57 if !isStart { 58 isStart = true 59 60 setButtonState(leftButton, isEnable: true) 61 62 self.leftButton.setTitle("记录", for: .normal) 63 self.rightButton.setTitle("停止", for: .normal) 64 65 self.time = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
RunLoop.current.add(self.time, forMode: .commonModes)
66 } else { 67 isStart = false 68 69 self.leftButton.setTitle("复位", for: .normal) 70 self.rightButton.setTitle("继续", for: .normal) 71 72 self.time.invalidate() 73 self.time = nil 74 } 75 } 76 77 override func didReceiveMemoryWarning() { 78 super.didReceiveMemoryWarning() 79 // Dispose of any resources that can be recreated. 80 } 81 } 82 83 extension ViewController { 84 func setupUI() { 85 86 UIApplication.shared.statusBarStyle = .lightContent 87 88 stringArr = [String]() 89 90 self.tableView.rowHeight = 40.0 91 self.leftButton.layer.cornerRadius = self.leftButton.frame.width / 2 92 self.rightButton.layer.cornerRadius = self.leftButton.frame.width / 2 93 94 setButtonState(self.leftButton, isEnable: false) 95 } 96 97 @objc func updateTime() { 98 if !isStart { 99 let tempStr = String(format: "%02d:%02d:%02d", 0, 0, 0) 100 self.timeLable.text = tempStr 101 } else { 102 msInt += 1 103 let tempStr = String(format: "%02d:%02d:%02d", msInt / 6000, (msInt / 100) % 60, msInt % 100) 104 self.timeLable.text = tempStr 105 } 106 } 107 108 func setButtonState(_ btn: UIButton, isEnable: Bool) { 109 btn.isEnabled = isEnable 110 if isEnable { 111 btn.alpha = 1.0 112 } else { 113 btn.alpha = 0.5 114 } 115 } 116 }
怎么样成为程序员,学习和实践,日积月累...