【问题标题】:How to persist edit mode of table view cell in table view even when table view cell is updated?即使更新表格视图单元格,如何在表格视图中保持表格视图单元格的编辑模式?
【发布时间】:2016-05-07 17:07:44
【问题描述】:

我目前正在使用 Apple 的 Swift 开发 iOS 应用程序。

我有一个表格视图,其中包含表格单元格,每个表格单元格都显示计时器的当前时间(它不是真正的计时器,它实际上只是一个时间戳)。

应用程序本身有一个计时器,它使用单元格计时器的当前状态更新表格视图的可见单元格。

该应用程序提供了滑动单元格的可能性,从而出现一个删除按钮。

我面临的问题是,由于单元格由应用程序的计时器更新,删除按钮立即消失。

这是更新表格视图单元格的代码:

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

如果有人对我的问题有解决方案/解决方法,我会很高兴。

提前谢谢你。

【问题讨论】:

  • 你能显示你的计时器更新单元格的代码吗?
  • @pbasdf 我已经更新了我的帖子。
  • 谢谢。避免使用reloadRowsAtIndexPaths:它会完全重建单元格,从而破坏“删除”状态。更新后的标签文本无需重新加载即可显示。
  • @pbasdf 你是绝对正确的。

标签: ios swift uitableview timer swift2


【解决方案1】:

我认为更简单的解决方案是删除您的reloadRowsAtIndexPaths 调用,然后更新timeLabel。你可以试试这个:

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

因为reloadRowsAtIndexPathsUITableView 说:嘿,只需在这些 indexPaths 中获取行并从头开始重建它们。忽略它之前的状态。

【讨论】: