我使用一個帶有可編輯的自定義樣式單元格的表視圖。在導航欄中有一個Edit按鈕,可以切換編輯模式。我需要編輯模式來洗掉和重新排列行數。
當我左劃一個單元格時,Delete操作按鈕顯示在該單元格的右側。當它顯示時,我按下Edit按鈕,Delete動作按鈕就消失了。再次按下Edit按鈕,顯示了左邊的洗掉命令和右邊所有(!)單元格的重新排序控制元件。到目前為止,一切都在意料之中。
現在(作為一個例子),當我左劃0號單元格,然后左劃1號單元格,而0號單元格的Delete操作按鈕仍然可見,用Edit按鈕激活編輯模式,現在顯示所有單元格的洗掉命令在左邊,重新排序控制在右邊,除了(!)0號單元格。
我發現,只要有2個或更多的單元格被左擦,第一個沒有被右擦的單元格就會脫離編輯模式,當用Edit按鈕啟用所有單元格的編輯模式時,會錯過洗掉控制(左邊)。更奇怪的是......在缺少洗掉控制(左)的主題單元格上,重新排序控制(右)顯示正確!
我跟隨并比較了許多單元格的編輯模式。
我遵循并比較了許多教程,但沒有發現這個錯誤。
我錯過了什么?
我錯過了什么?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
...。
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...。
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
...。
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
...。
}
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
...。
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
...。
}
//沒有必要 = 沒有改變任何東西,但在許多教程中發現。
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
}
@IBAction func editAction(_ sender。Any) { //那是編輯按鈕的動作。
self.setEditing(! isEditing, animated: true)
self.navigationItem.leftBarButtonItem?. title = isEditing ? "Done" : "Edit"。
}
在所有涉及的方法之上。不相關的方法內容被省略了,以保持帖子的緊湊。如果認為有必要,我將添加/編輯。

左滑單元格2,然后單元格1,然后使用Edit按鈕進入編輯模式后的視圖。
uj5u.com熱心網友回復:
我找到了錯誤行為的原因。因為我在setEditing()方法中使用了影片,我需要用beginUpdates()和endUpdates()告訴視圖有更新。這就完成了作業! 無論是模擬器還是設備,都不再有糟糕的單元格顯示了!
@IBAction func editAction(_ sender: Any) {
tableView.beginUpdates()
self.setEditing(! isEditing, animated: true)
tableView.endUpdates()
self.navigationItem.leftBarButtonItem?. title = isEditing ? "Done" : "Edit"。
}
編輯1
上面(beginUpdates()和endUpdates())可以如愿以償,但下面(performBatchUpdates {})更現代(iOS 11.0),應該是首選。
@IBAction func editAction(_ sender: Any) {
tableView.performBatchUpdates {
self.setEditing(! isEditing, animated: true)
}
self.navigationItem.leftBarButtonItem?。 title = isEditing ? "Done" : "Edit"。
}
編輯2
同時,我相信OP的錯誤行為是一個iOS的bug。
偶然間,我發現 Safari 書簽表視圖的行為與此相同。左劃0號單元格,然后左劃1號單元格,接下來按Done/Edit按鈕(右下角)兩次。你可以觀察到,0號單元格的左側沒有顯示洗掉控制圖示(單向符號),因此我認為這是一個iOS的錯誤。
除此之外,其他應用程式在編輯表視圖時也產生了同樣的錯誤行為。
uj5u.com熱心網友回復:
嗨,我相信你的問題可以通過使用UITableView的預設editButtonItem來解決。
navigationItem.leftBarButtonItem = self.editButtonItem
這將按鈕的狀態設定為編輯模式,當一個滑動動作被執行時,因此應該可以解決你的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311751.html
標籤:

