简体   繁体   English

如何在isEditing属性更改时更新UITableView?

[英]How to update UITableView when isEditing property changes?

I've currently got a TableViewController with a navigation bar with an editButtonItem. 我目前有一个带有带editButtonItem的导航栏的TableViewController。 My tableview correctly goes into editing mode when I hit this button, but I want to change the labels on my custom UITableViewCell when isEditing changes. 当我点击此按钮时,我的tableview正确进入编辑模式,但我想在isEditing更改时更改自定义UITableViewCell上的标签。 I've tried adding the following into cellForRowAt: 我已经尝试将以下内容添加到cellForRowAt中:

   if editingMode {
        cell.timeZoneLabel = ""
    } else {
        cell.timeZoneLabel = timeZone.city
    }

But it seems that the tableView isn't reloaded when isEditing changes. 但是当isEditing发生变化时,似乎没有重新加载tableView。

My next thought was to set the following variable at the start: 我的下一个想法是在开始时设置以下变量:

var editingMode = false {
    didSet {
        tableView.reloadData()
    }
}

And add the following to viewDidLoad() so I can reload the tableView when isEditing changes: 并将以下内容添加到viewDidLoad()中,以便在isEditing更改时重新加载tableView:

    editingMode = isEditing

This doesn't seem to work either though. 但这似乎不起作用。 I've tried searching for other solutions but I can't find anyone having the similar problem. 我试过寻找其他解决方案,但我找不到有类似问题的人。

Any guidance would be much appreciated! 任何指导将不胜感激!

In the cell override didTransition(to . The rawValue 0 represents normal state, rawValue 1 is showingEditControl 在单元格覆盖中didTransition(to rawValue 0表示正常状态, rawValue 1表示showingEditControl

override func didTransition(to state: UITableViewCell.StateMask) {
    switch state.rawValue {
    case 0: timeZoneLabel = timeZone.city
    case 1: timeZoneLabel = ""
    default: break
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM