代码之家  ›  专栏  ›  技术社区  ›  CoderJ13

删除视图控制器中具有多个表视图的uitableview行(swift)

  •  1
  • CoderJ13  · 技术社区  · 6 年前

    我正试图在viewcontroller中包含多个uitableviews的一个tableview中实现一个delete行。我的数组是用nsuserdefaults保存的。 这是我的代码,但我发现了错误 Index out of range 如果有人想测试我的项目,这里还有一个链接: https://files.fm/f/wv9uerhn

     func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    
        if editingStyle == .delete {
    
        if tableView == ScheduleTableView{
    
           // Here is my code but it gives me an error, first you need to add a row with the add button
    
            let defaults = UserDefaults.standard
            var myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
    
            print(myarray)
            myarray.remove(at: indexPath.row)
            ScheduleTableView.deleteRows(at: [indexPath], with: .fade)
    
    
        }
    
        if tableView == GoalsTableView{
    
    
    
        }
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Md. Ibrahim Hassan    6 年前

    实际上,您并没有将带有已删除记录的数组保存为userdefaults。将代码更新为

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    {
            if editingStyle == .delete {
    
            if tableView == ScheduleTableView{
    
               // Here is my code but it gives me an error, first you need to add a row with the add button
    
                let defaults = UserDefaults.standard
                var myarray = defaults.stringArray(forKey: "ScheduleArray") ?? [String]()
    
                print(myarray)
                myarray.remove(at: indexPath.row)
                defaults.set(myarray, forKey: "ScheduleArray")
                ScheduleTableView.deleteRows(at: [indexPath], with: .fade)
    
    
            }
    
            if tableView == GoalsTableView{
    
    
    
            }
        }
      }
    }
    

    希望这有帮助。快乐的编码。