Change default background color while deleting a row from tableView

China☆狼群 提交于 2020-01-15 09:19:29

问题


In my app, I changed the background color of every view to dark. But when I delete a row from tableView, row background changed to white. Why and how can I fix it?

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

回答1:


Found a workaround from Realm Swift document.

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

They're using async to trigger the updates. So I think we just need to avoid calling delete, insert or update methods in the scope of UITableViewDelegate.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        boards.remove(at: indexPath.row)
        // use async to avoid showing white background.
        DispatchQueue.main.async {
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }
}



回答2:


You have add below single line code in commit editingStyle method may be it's help you!!

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

Or simple use following code:

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



回答3:


Change the background of the cell programmatically using:

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


来源:https://stackoverflow.com/questions/47102469/change-default-background-color-while-deleting-a-row-from-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!