pierreafranck pierreafranck - 2 years ago 80
Swift Question

Swipe to delete doesn't work with Timer

I have implement the option "Swipe to delete" in my TableView like this:

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()
}
}


It works fine before the implementation of my 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()
}


EDIT:

enter image description here

Now, the swipe to delete just doesn't work. Nothing append when I swipe.

How can I do it works again?

Answer Source

Update

  1. remove reloadData in updateTimer
  2. use my code to display your rows[j]["time"].

My workable way

Origin

If you want to show your timer, you can use this:

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"])"
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download