前提
構造体を使用してデータを管理しています。
実現したいこと
セルをスワイプして構造体データの中から特定の行を削除したいです
発生している問題・エラーメッセージ
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
// 構造体 struct User: Codable { let name: String let url: String let profile_image_url: String } // MARK: - UITableview func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return List.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "EditTableViewCell", for: indexPath) as! EditTableViewCell // self.List = getValue() cell.idLabel.text = "\(indexPath.row + 1)" cell.titleLabel.text = List[indexPath.row].name cell.name.text = List[indexPath.row].url let urlString = List[indexPath.row].profile_image_url Nuke.loadImage(with: URL(string: urlString)!, into: cell.userImage) return cell } // 高さ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ return 72 } // 空のテーブル表示用 func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! { return NSAttributedString(string: "データがありません") } // タップ /* 省略 */ // セルの削除 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { List.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.automatic) // 追加:削除した内容を保存 let encoder = JSONEncoder() if let encodedValue = try? encoder.encode(self.List) { UserDefaults.standard.set(encodedValue, forKey: "List") self.save() print("削除なう") print(self.List.count) } } // MARK: - Functions private func getValue() -> [User] { guard let data = UserDefaults.standard.data(forKey: "List") else { return [] } return decoded(data: data) } private func decoded(data: Data) -> [User] { do { let jsonDecoder = JSONDecoder() let followes = try jsonDecoder.decode([User].self, from: data) return followes } catch { return [] } } private func save() { guard let data = encoded() else { return } UserDefaults.standard.setValue(data, forKey: "TwitterUserList") print("保存完了しました") } private func encoded() -> Data? { do { let encoder = JSONEncoder() let data = try encoder.encode(self.userList) return data } catch { return nil } }
試したこと
・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
まだ回答がついていません
会員登録して回答してみよう