mian mian - 7 months ago 19
Swift Question

How to persist edit mode of table view cell in table view even when table view cell is updated?

I am currently developing an iOS application in Apple's Swift.

I have a table view with table cells in it each of which displays the current time of a timer (it is not a real timer it is actually just a timestamp).

The application itself has a timer which updates the visible cell of the table view with the current states of the cell timers.

The application provides the possiblity to slide a cell which lets appear a delete button.

The problem I am faced with is that the delete button immediately disappears due to the fact that the cell is updated by the application's timer.

Here is the code where the table view cells are updated:

// Update visible rows in table
func updateTable() {
// Get all visible cells
let cells = timerTable.visibleCells as! Array<TimerTableViewCell>

for cell in cells {
let indexPath = timerTable.indexPathForCell(cell)
cell.timeLabel.text = String(timerArray[indexPath!.row].getRemainingTimeAsString())
timerTable.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.None)
}
}


I would be glad if anyone would have a solution/workaround for my problem.

Thank you in advance.

Answer

I think the simpler solution is to remove your reloadRowsAtIndexPaths call, and just update the timeLabel. You could try this:

// Update visible rows in table
func updateTable() {
    // Get all visible cells
    let cells = timerTable.visibleCells as! Array<TimerTableViewCell>

    for cell in cells {
        let indexPath = timerTable.indexPathForCell(cell)
        cell.timeLabel.text = String(timerArray[indexPath!.row].getRemainingTimeAsString())
    }
}

Because reloadRowsAtIndexPaths says to the UITableView: Hey, just take the rows at theses indexPaths and rebuild them from scratch. Ignore its previous state.