2017-11-04 91 views
1

刪除行我看到的問題是,當我創建一個UIContextualAction.destructivetruecompletionHandler似乎有去除該行默認動作。UIContextualAction具有破壞性的風格似乎默認

如果您從Xcode的模板創建一個新的主從應用程序和MasterViewController添加此代碼...

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 
    let testAction = UIContextualAction(style: .destructive, title: "Test") { (_, _, completionHandler) in 
     print("test") 
     completionHandler(true) 
    } 
    return UISwipeActionsConfiguration(actions: [testAction]) 
} 

你刷卡的行會被刪除。請注意,在那裏沒有更新表格視圖的代碼。此外,該模型不會更新,如果您向上滾動以使該行重新加載,它將重新出現。

傳遞false在這種情況下不會刪除該行。或者使用.normal風格和true也不會刪除該行。

.destructivetrue導致該行在默認情況下被刪除。

任何人都可以解釋這種行爲嗎?爲什麼該行被刪除?

回答

0

每相消選擇文檔:

一個刪除數據或執行某些類型的破壞性影響的任務的行動。

完成意味着表示行動是否成功。傳遞真實意味着破壞性的任務是成功的,因此應該刪除這一行。

您意圖在發生破壞性操作時手動更新您的數據源,而不這樣做會導致滾動以使數據重新出現。您還需要告訴tableView數據已被刪除。

下面是表示工作示例一些代碼:

UIContextualAction(style: .destructive, title: "Delete") { [weak self] (_, _, completion) in 
    if self?.canDelete(indexPath) { // We can actually delete 
     // remove the object from the data source 
     self?.myData.remove(at: indexPath.row) 
     // delete the row. Without deleting the row or reloading the 
     // tableview, the index will be off in future swipes 
     self?.tableView?.deleteRows(at: [indexPath], with: .none) 
     // Let the action know it was a success. In this case the 
     // tableview will animate the cell removal with the swipe 
     completion(true)  
    } else { // We can't delete for some reason 
     // This resets the swipe state and nothing is removed from 
     // the screen visually. 
     completion(false) 
    }   
} 

這是一個有點混亂,我唯一的部分需要重新加載tableview中,或致電deleteRows爲了有indexPath正確計算的下一次滑動。

如果我有10行,並且第五個要刪除,那麼之後的所有人都將被關閉一行,除非tableview被重新加載或者tableview被告知被刪除的行以某種方式被刪除。