Swift 定时器

Github地址:https://github.com/LINGLemon/LXFSwiftApp

import UIKit

class TimerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    let dataList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21,]

    @IBOutlet weak var tableView: UITableView!
    var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        startTimer()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        invalidateTimer()
    }
    
    // MARK: - 定时器相关
    
    func startTimer() {
        print("startTimer")
        timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (_) in
            print("gogogo")
        })
        // TableView的拖动不会导致定时器暂停
        RunLoop.current.add(timer!, forMode: .common)
    }
    
    func invalidateTimer() {
        if timer != nil {
            timer!.invalidate()
            timer = nil
            print("invalidateTimer")
        }
    }
    
    // MARK: - UITableViewDataSource
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self), for: indexPath)
        let data = dataList[indexPath.row]
        cell.textLabel?.text = "\(data)"
        
        return cell
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60;
    }
    
}

 

你可能感兴趣的:(swift,iOS开发)