2017-11-04 4 views
1

で行を削除するようで、私が見ている問題は、私はcompletionHandler.destructiveUIContextualActionパスtrueを作成するときに、行を削除するためのデフォルトのアクションがあるように思われるということです。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も行を削除しません。

.destructiveおよびの結果、デフォルトで行が削除されます。

誰でもこの動作を説明できますか?行が削除されるのはなぜですか?

破壊的なオプションのドキュメント毎の

答えて

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) 
    }   
} 

一部だけがテーブルビューをリロードするか、indexPathが適切に計算されていためにdeleteRowsを呼び出すために必要とされます次のスワイプ。

もし私が10行を持っていて、削除する5番目のものをスワイプすると、それ以降の人はtableviewが再ロードされたり、何らかの方法で削除された行のtableviewが読み込まれたりしない限り、

関連する問題