从tableView中删除行时更改默认背景颜色

时间:2017-11-03 18:37:39

标签: ios swift uitableview

在我的应用中,我将每个视图的背景颜色更改为黑暗。但是当我从tableView中删除一行时,行背景变为白色。为什么以及如何解决?

Screenshot

Reveal

tableView.backgroundView?.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0)
        view.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0)
        if dataManagement.taskData.count != 0{
            for rowIndex in 0...tableView.numberOfRows(inSection: 0) - 1 {
                let cellPath = IndexPath(row: rowIndex, section: 0)
                if let cell = tableView.cellForRow(at: cellPath) as? TaskListTableViewCell {
                    cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0)
                    cell.taskLabel.textColor = UIColor.white
                }
            }
        }

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { //可删除
    let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskListTableViewCell
    if editingStyle == UITableViewCellEditingStyle.delete {
        if dataManagement.taskData[(indexPath as NSIndexPath).row].id == dataManagement.currentTask {
            dataManagement.currentTask = nil
            dataManagement.historyIsSelected = true
            pomodoroTimer.stop()
        }
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
    }
}

3 个答案:

答案 0 :(得分:2)

从Realm Swift文档中找到了一种解决方法。

https://realm.io/docs/swift/latest/#realm-notifications

他们使用异步来触发更新。 所以我认为我们只需要避免在pip install tensorflow-1.5.0-cp36-cp36m-win_amd64.whl范围内调用deleteinsertupdate方法。

UITableViewDelegate

答案 1 :(得分:0)

使用以下方式以编程方式更改单元格的背景:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor <yourcolor>];
} 

答案 2 :(得分:0)

您已在提交editStyle 方法中添加以下单行代码,可能会帮助您!!

cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0)

或者简单使用以下代码:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { //可删除
    let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskListTableViewCell
    cell.backgroundColor = UIColor(red:0.1922, green:0.1922, blue:0.1922, alpha:1.0) // set cell background color here!!

    if editingStyle == UITableViewCellEditingStyle.delete {
        if dataManagement.taskData[(indexPath as NSIndexPath).row].id == dataManagement.currentTask {
            dataManagement.currentTask = nil
            dataManagement.historyIsSelected = true
            pomodoroTimer.stop()
        }
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
   }
}
相关问题