【发布时间】:2021-10-21 09:33:17
【问题描述】:
我有以下问题。如果我只滑动单元格直到删除按钮可见,然后单击编辑,则表格未处于编辑模式,但按钮显示它。
我已经使用shouldBeginEditingAtRow 和didEndEditingAtRow 方法看到,如果我先滑动一行直到删除按钮,然后再滑动下一行,tableView.isEditing 的状态为true。
这意味着如果我滑动一行直到删除按钮可见,然后单击编辑,setEditing 会将 tableView 的状态设置为false。
我已经尝试在didEndEditingAtRow 方法中将状态设置为false。但不幸的是,这不起作用。你有什么建议或解决方案吗?
我的代码:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var data = [1,2,3,4,5,6,7,8,9]
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(!isEditing, animated: true)
tableView.setEditing(!tableView.isEditing, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = String(data[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
navigationItem.rightBarButtonItem = editButtonItem
}
}
【问题讨论】:
标签: ios swift xcode uitableview editing