2017-10-23 6 views
0

行を削除してデータマネージャでも削除したいのですが、どのようにしてインデックスパスで削除するのですか?私はUserDefraultsを削除するindexPath.rowをどのように削除するのですか?

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

     self.tableView.beginUpdates() 

     print("Deleted was pressed") 

     records.remove(at: indexPath.row) 

     tableView.deleteRows(at: [indexPath], with: .automatic) 

     // DataManager.getInstance().setNewRecord(array: [UserRecordDataManager]) 
    //  DataManager.getInstance().setNewRecord(array: UserRecordDataManager) 
     self.tableView.endUpdates() 
     tableView.reloadData() 
     print(records.count) 

    } 
} 

/// Data Manager- doing the SET option 

    public func setNewRecord(array: [UserRecordDataManager]){ 

      records.remove=??? 
      self.records = getRecords() 
     print(DataManager.getInstance().records.count) 

     } 

答えて

0
in the data manager: 

    public func removeRecord(index: Int){ 
      if getRecords().isEmpty{ 
       print("ERROR - there is no recoreds") 
       return 
      }else{ 
       self.records = getRecords() 
       self.records.remove(at: index) 

       let encodeData = NSKeyedArchiver.archivedData(withRootObject: self.records) 
       UserDefaults.standard.set(encodeData, forKey: "records") 
      } 
     } 




in the tableview: 

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

      self.tableView.beginUpdates() 

      print("Deleted was pressed") 

      let index = indexPath.row 

      records.remove(at: index) 

      print(index) 
      tableView.deleteRows(at: [indexPath], with: .automatic) 
      DataManager.getInstance().removeRecord(index: index) 

      self.tableView.endUpdates() 
      tableView.reloadData() 
      print(records.count) 

    } 
関連する問題