前提
構造体を使用してデータを管理しています。
実現したいこと
セルをスワイプして構造体データの中から特定の行を削除したいです
発生している問題・エラーメッセージ
Thread 1: "Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (99) must be equal to the number of rows contained in that section before the update (99), 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)."
スレッド1:「無効な更新:セクション0の行数が無効です。更新後の既存のセクションに含まれる行数(99)は、更新前のそのセクションに含まれる行数(99)に加えて、または、そのセクションに挿入または削除された行数(0が挿入、1が削除)、およびプラスまたはマイナスがそのセクションに移動または削除された行数(0が移動、0が移動)。」
該当のソースコード
Swift
1// 構造体 2struct User: Codable { 3 let name: String 4 let url: String 5 let profile_image_url: String 6 } 7 8// MARK: - UITableview 9 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 10 return List.count 11 } 12 13 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 14 15 let cell = tableView.dequeueReusableCell(withIdentifier: "EditTableViewCell", for: indexPath) as! EditTableViewCell 16 17 // self.List = getValue() 18 19 cell.idLabel.text = "\(indexPath.row + 1)" 20 cell.titleLabel.text = List[indexPath.row].name 21 cell.name.text = List[indexPath.row].url 22 23 let urlString = List[indexPath.row].profile_image_url 24 Nuke.loadImage(with: URL(string: urlString)!, into: cell.userImage) 25 26 return cell 27 } 28 29 // 高さ 30 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 31 return 72 32 } 33 34 // 空のテーブル表示用 35 func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! { 36 return NSAttributedString(string: "データがありません") 37 } 38 39 // タップ 40 /* 省略 */ 41 42 // セルの削除 43 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 44 45 List.remove(at: indexPath.row) 46 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic) 47 // 追加:削除した内容を保存 48 let encoder = JSONEncoder() 49 if let encodedValue = try? encoder.encode(self.List) { 50 UserDefaults.standard.set(encodedValue, forKey: "List") 51 self.save() 52 print("削除なう") 53 print(self.List.count) 54 } 55 } 56 57// MARK: - Functions 58 private func getValue() -> [User] { 59 guard let data = UserDefaults.standard.data(forKey: "List") else { 60 return [] 61 } 62 return decoded(data: data) 63 } 64 65 private func decoded(data: Data) -> [User] { 66 do { 67 let jsonDecoder = JSONDecoder() 68 let followes = try jsonDecoder.decode([User].self, from: data) 69 return followes 70 } catch { 71 return [] 72 } 73 } 74 75private func save() { 76 guard let data = encoded() else { return } 77 UserDefaults.standard.setValue(data, forKey: "TwitterUserList") 78 print("保存完了しました") 79 } 80 81 private func encoded() -> Data? { 82 do { 83 let encoder = JSONEncoder() 84 let data = try encoder.encode(self.userList) 85 return data 86 } catch { 87 return nil 88 } 89 } 90
試したこと
・swift4 - スワイプでtableviewのセル削除
https://qiita.com/Lulu34/items/b0c88d1e1163d50f743b
List と userList という 2 つの変数を使ってるせいでは…。
元のコードがuserListで質問するにあたってListに置換しました。
一部漏れがあったので全てListに修正し直しました。
よろしくお願いいたします。
それで動くことは動くはずですけど…。データを保存する処理がメソッド化されてないのが気になりますが、もしかして追加の際に保存を忘れてるとか?
なお、変数名は userList の方が良いですし、cellForRowAt の中で List を書き換える (同じ値になるはずとはいえ) のは握手です。
> それで動くことは動くはずですけど…。データを保存する処理がメソッド化されてないのが気になりますが、もしかして追加の際に保存を忘れてるとか?
こちらも修正しました。
(save関連を一度外してデバッグしてました。元に戻しました)
現在このバージョンでも削除すると落ちてしまいます、、
まさかの直りましたm(_ _)m
cellForRowAt内の 『self.List = getValue()』が余計でした。
大変失礼しましたm(_ _)m
回答1件
あなたの回答
tips
プレビュー