滑动即可删除不适用于Timer

时间:2017-07-27 07:51:07

标签: swift

我已实施选项"滑动以删除"在我的TableView中像这样:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  return true
}

func tableView(_ tableView: UITableView, commit editingStyle: 
 UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
    rows.remove(at: indexPath.row)
    runnerTableView.deleteRows(at: [indexPath], with: .fade)
    runnerTableView.reloadData()
  }
}

在我的Timer实现之前它工作正常:

timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, 
 selector:#selector(ViewController.updateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

func updateTimer () {
  var j = 0
  for _ in rows {
    if (rows[j]["Playing"] as! Bool == true ) {
      rows[j]["time"] = (rows[j]["time"] as! Double + 0.01) as AnyObject
    }
    j += 1
  }
  runnerTableView.reloadData()
}

编辑:

enter image description here

现在,要删除的滑动不起作用。我刷卡时没有任何附加物。

我怎样才能再次运作?

2 个答案:

答案 0 :(得分:1)

<强>更新

  1. 删除reloadData
  2. 中的updateTimer
  3. 使用我的代码显示您的rows[j]["time"]
  4. My workable way

    <强>来源

    如果您想显示计时器,可以使用:

    func updateTimer() {
        var j = 0
        for _ in rows {
            if (rows[j]["Playing"] as! Bool == true ) {
                rows[j]["time"] = (rows[j]["time"] as! Double + 0.01) as AnyObject
            }
            j += 1
        }
    
        for cell in tableView.visibleCells {
            if let indexPath = tableView.indexPath(for: cell) {
                cell.timeLabel.text = "\(rows[indexPath.row]["time"])"
            }
        }
    }
    

答案 1 :(得分:0)

我只是在添加以下内容时才会看到您的问题:RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)。这可能是因为它在主线程中运行,因此阻塞了tableView UI。我在viewDidLoad添加了计时器。

尝试将此行替换为: RunLoop.current.add(self.timer, forMode: RunLoopMode.commonModes)并移除reloadData()函数中的updateTimer

相关问题