質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

6233閲覧

UITableViewの削除の際のエラーを解決したい

wakkamassa

総合スコア14

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/07/04 07:55

UITableViewのセルの削除でのエラーを解決したい。

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)

のメソッドを使用してセルの削除を行おうとしたのですがエラーが出てしまいます。

セルの数のところは

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArray.count
}

としているのですが、 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

で初めにbuildしてしまったためエラーが出てしまっていると考えています。

発生している問題・エラーメッセージ

'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

該当のソースコード

import UIKit import RealmSwift class TaskViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet weak var tableView: UITableView! var task:Task! let realm = try! Realm() lazy var predicate = NSPredicate(format:"categoryName ==%@",task.categoryName) lazy var taskArray = try! Realm().objects(Task.self).sorted(byKeyPath:"id").filter(self.predicate) override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let task = taskArray[indexPath.row] cell.textLabel?.text = task.name let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm" let dateString = formatter.string(from: task.date) cell.detailTextLabel?.text = dateString return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return taskArray.count } func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { return .delete } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete{ try! realm.write { tableView.deleteRows(at: [indexPath], with: .fade) realm.delete(taskArray[indexPath.row]) } } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "cellSegue", sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let createTaskViewController:CreateTaskViewController = segue.destination as! CreateTaskViewController if segue.identifier == "cellSegue"{ let indexPath = self.tableView.indexPathForSelectedRow createTaskViewController.task = taskArray[(indexPath?.row)!] } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tableView.reloadData() } @IBAction func createNewTask(_ sender: UIBarButtonItem) { if taskArray.count != 0{ self.task.id = taskArray.max(ofProperty: "id")! + 1 } let createNewTaskViewController = self.storyboard?.instantiateViewController(withIdentifier: "newTask") as! createNewTaskViewController self.present(createNewTaskViewController,animated:true,completion:{ createNewTaskViewController.task = self.task }) }}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/07/04 08:25

セルをスワイプして削除したいのでしょうか?
wakkamassa

2018/07/04 08:45

スワイプして削除したいです。
guest

回答1

0

ベストアンサー

セルを直接削除するのではなく、削除したい要素をtaskArrayから削除した後、テーブルをリロードして下さい。

投稿2018/07/04 08:16

fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wakkamassa

2018/07/04 08:49

削除したい要素をtaskArrayから削除した後、テーブルをリロードすることによって表示することができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問