如何创建UITableView,每隔5秒添加一行时间戳?

时间:2018-03-27 08:33:49

标签: ios iphone swift xcode uitableview

Screen

如何创建一个UITableView,每隔5秒添加一行,点击开始按钮时会显示时间戳,点击停止按钮时会停止?

    @IBAction func addCellTapped(_ sender: Any) {
    dataArray.append("Item \(dataArray.count + 1)")

    let insertionIndexPath = IndexPath(row: dataArray.count - 1, section: 0)
    tableView.insertRows(at: [insertionIndexPath as IndexPath], with: .automatic)
}

@IBAction func stopCellTapped(_ sender: Any) {
}

2 个答案:

答案 0 :(得分:1)

您只需要在数组中追加数据并在numberOfRows中返回该数组。试试这个。如果要在新行插入后将TableView滚动到底部,请调用scrollToBottom函数,如下所示。

将计时器定义为全局。 var timer = Timer()

func startTimer() {
    timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
}

@objc func update() {
    arrData.add(arrData.count + 1)
    self.tblVW.reloadData()
    self.scrollToBottom()
}

func scrollToBottom() {
    DispatchQueue.main.async {
        let indexPath = IndexPath(row: self.arrData.count-1, section: 0)
        self.tblVW.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

@IBAction func addCellTapped(_ sender: Any) {
    if timer.isValid { // CHECK IF TIMER IS VALID THAN INVALIDATE IT
        timer.invalidate()
    }
    else {
        self.startTimer()
    }
}

@IBAction func stopCellTapped(_ sender: Any) {
    timer.invalidate()
}

答案 1 :(得分:1)

在此任务中使用Timer(考虑到答案Kuldeep

class ViewController: UIViewController {

   @IBOutlet weak var tableView: UITableView!

   var timeArray: [TimeInterval] = []
   var timer = Timer()

   override func viewDidLoad() {
       super.viewDidLoad()

       tableView.tableFooterView = UIView()
   }

   func startTimer() {
       timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(updateTableView), userInfo: nil, repeats: true)
   }

   func stopTimer() {
       timer.invalidate()
   }

   @objc func updateTableView() {

       let lastTimerValue = timeArray.last ?? 0

       timeArray.append(lastTimerValue + timer.timeInterval)
       tableView.reloadData()
       scrollToBottom()
   }

   func scrollToBottom() {
       DispatchQueue.main.async {
           let indexPath = IndexPath(row: self.timeArray.count - 1, section: 0)
           self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
       }
   }

   @IBAction func startClicked(_ sender: Any) {
       if timer.isValid {
           return
       } else {
           startTimer()
       }
   }

   @IBAction func stopClicked(_ sender: Any) {
       stopTimer()
   }
}

extension ViewController: UITableViewDataSource {

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return timeArray.count
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       let cellIdentifier = "Cell"
       let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

       cell.textLabel?.text = String(timeArray[indexPath.row])
       cell.textLabel?.textAlignment = .center

       return cell
   }
}