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 }) }}
回答1件
あなたの回答
tips
プレビュー