2017-07-27 74 views
0

工作,我必须实现“刷卡删除”我的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.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

现在,要删除的滑动功能不起作用。当我滑动时没有附加内容。

我该怎么办呢?

+1

1.我复制你的代码,它的工作原理。 2.我实现你的计时器,它的工作原理。 你在'updateTimer'中做了什么? – Codus

+1

你不应该重新加载你的'tableview'! – Codus

+0

@coded updateTimer的函数? – pierreafranck

回答

1

更新

  1. 删除reloadDataupdateTimer
  2. 用我的代码,以显示你的rows[j]["time"]

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"])" 
     } 
    } 
} 
+0

我不想显示我的计时器,这已经完成了,我正在尝试做一个显示问题的gif – pierreafranck

+0

如果你不想显示你的计时器,没有必要' 'rows [j] [“time”]'后面的reloadData'改变了 – Codus

+0

它已经完成了,我展示它!但我可以尝试你的方法显示 – pierreafranck

0

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

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

+0

我这样做是因为如果我滚动我的TableView,定时器在所有滚动时间内停止 – pierreafranck

+0

好的,在这种情况下,您可以将它添加到'viewDidLoad'中的当前runloop。也许你的时间间隔太短。 –

+0

它已经在viewDidLoad – pierreafranck