我有以下問題。如果我只輕掃單元格,直到看到洗掉按鈕,然后點擊編輯,那么表格就不在編輯模式中,但按鈕卻顯示出來了。
我已經通過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()
///在加載視圖后做任何額外的設定。
tableView.dataSource = self
tableView.delegate = self
navigationItem.rightBarButtonItem = editButtonItem
}
}
uj5u.com熱心網友回復:
首先,當你重寫一個UIKit方法時,盡量從你重寫的方法中傳遞引數,所以用這幾行來更新setEditing方法:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
//hhandle Animationif editing {
self.tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
}
tableView.setEditing(editing, animated: true)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311729.html
標籤:



