[swift4]UITableViewのセルをスワイプで削除する方法

UITableViewのセルをスワイプで削除する方法は以下のコードを実装する

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
 if editingStyle == .delete {
 sourceArray.remove(at: indexPath.row)
 tableView.deleteRows(at: [indexPath], with: .fade)
 }
}

sourceArrayはデータソース

データソースを変更する前にtableView.deleteRowsを実行するとクラッシュするので注意

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です