2017-07-27 72 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