【问题标题】:TableView - Cannot delete more than one cellTableView - 不能删除多个单元格
【发布时间】:2017-07-12 20:16:22
【问题描述】:

数据存储有问题。如果我有 5 个单元格,顺便说一下,我希望每个单元格之间有空格,所以从技术上讲,每个单元格都是一个有一行的部分……我删除了第二个、第三个和第四个单元格,它首先会出现我删除了那些单元格,但是当我再次打开应用程序时,结果发现我只删除了第二个单元格(我决定删除第一个单元格)。

如何一次删除多个单元格?

我的删除码:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        todos.remove(at: indexPath.section)

       // tableView.deleteSections(NSIndexSet(index: indexPath), with: .Automatic)
       // tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)
        let defaults = UserDefaults.standard
        if todos.count >= 0 {
            // Save what we have
            let data = NSKeyedArchiver.archivedData(withRootObject: todos)
            defaults.set(data, forKey: "TDDATA")
            defaults.synchronize()
            print("saved \(todos.count)")
        } else if let storedTodoData = defaults.data(forKey: "TDDATA"),
            let storedTodos = NSKeyedUnarchiver.unarchiveObject(with: storedTodoData) as? [TodoModel] {
            // There was stored data! Use it!
            todos = storedTodos
            print("Used \(todos.count) stored todos")
        }

        tableView.reloadData()


    }
}

当我打开应用程序时:

override func viewDidAppear(_ animated: Bool) {
    print("i think it worked...")

    let defaults = UserDefaults.standard
    if todos.count > 0 {
        // Save what we have
        let data = NSKeyedArchiver.archivedData(withRootObject: todos)
        defaults.set(data, forKey: "TDDATA")
        defaults.synchronize()
        print("saved \(todos.count)")
    } else if let storedTodoData = defaults.data(forKey: "TDDATA"),
        let storedTodos = NSKeyedUnarchiver.unarchiveObject(with: storedTodoData) as? [TodoModel] {
        // There was stored data! Use it!
        todos = storedTodos
        print("Used \(todos.count) stored todos")

        tableView.reloadData()
        self.tableView.tableFooterView = UIView()
    }
   //print([todos.first])
    print("Here?: \(todos.first?.title)")
    tableView.reloadData()
}

【问题讨论】:

  • 仅供参考 - 不要将您的数据存储在用户默认值中。将其写入文件。
  • 你为什么有if todos.count >= 0支票?计数不能为负数。
  • 这也是我打开应用的加载方式。我尝试将其更改为 > 但它不起作用。有什么建议么? @rmaddy
  • 这不是你的加载方法。仅当您删除一行并且数组中的对象现在少于零时才会调用它。显然,这甚至是不正确的。在 viewDidLoad 方法中加载数据。
  • @rmaddy >= 0 todos 部分在我的加载方法中。当然,单元格删除部分也不会出错。

标签: ios arrays swift uitableview tableview


【解决方案1】:

试试这个:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {


    let delete: UITableViewRowAction = UITableViewRowAction(style: .default, title: "Eliminar", handler: {
        (rowAction: UITableViewRowAction, indexPath: IndexPath) in

        todos.remove(at: indexPath.section)

        // tableView.deleteSections(NSIndexSet(index: indexPath), with: .Automatic)
       // tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.automatic)
        let defaults = UserDefaults.standard
        if todos.count >= 0 {
           // Save what we have
           let data = NSKeyedArchiver.archivedData(withRootObject: todos)
           defaults.set(data, forKey: "TDDATA")
           defaults.synchronize()
           print("saved \(todos.count)")
       } else if let storedTodoData = defaults.data(forKey: "TDDATA"),
           let storedTodos = NSKeyedUnarchiver.unarchiveObject(with: storedTodoData) as? [TodoModel] {
           // There was stored data! Use it!
           todos = storedTodos
           print("Used \(todos.count) stored todos")
       }

       tableView.reloadData()

    })

    return [delete]



}

【讨论】:

  • 这如何解决问题(它没有)?答案应该解释问题是什么以及答案如何解决问题。用“试试这个”发布代码不是一个有用的答案。
  • todos 是一个数组吗?
  • 是的。但这并没有在我尝试时删除任何内容
  • 好吧。首先尝试 removeItem 和 ReloadData()。它应该更新表格。如果可行,您可能需要取消归档数据并再次归档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多