2015-10-18 72 views
0

我正在做一個筆記應用程序,並添加了滑動刪除行方法。我遇到的問題是當表格視圖中保存了多個筆記,並且我滑動一行以刪除它時會刪除所有筆記。另外,當我退出應用程序並返回時,便籤又回到桌子視圖中。下面是我的代碼:爲什麼當我在tableview中刪除一行時,它會刪除多行?

class MasterViewController: UITableViewController { 
    var notesItems: NSMutableArray = NSMutableArray() 

    override func viewDidAppear(animated: Bool) { 
     let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults() 
     let itemListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray 

     if ((itemListFromUserDefaults) != nil) { 
      notesItems = itemListFromUserDefaults! 
     } 

     self.tableView.reloadData() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.navigationController?.setNavigationBarHidden(false, animated: true) 
     self.navigationController?.toolbarHidden = false 

     self.tableView.dataSource = self 

     UINavigationBar.appearance().barTintColor = UIColor.orangeColor() 
     UIToolbar.appearance().barTintColor = UIColor.orangeColor() 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return notesItems.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     let notesItem:NSDictionary = notesItems.objectAtIndex(indexPath.row) as! NSDictionary 

     cell.textLabel?.text = notesItem.objectForKey("text") as? String 

     return cell 
    } 

    // Override to support editing the table view. 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == UITableViewCellEditingStyle.Delete { 
      self.tableView.reloadData() 

      self.tableView.beginUpdates() 

      self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 

      // Delete the row from the data source 
     } 
    } 
} 

回答

2

您在commitEditingStyle中的代碼都是錯誤的。

  1. 請勿重新加載表格視圖。
  2. 在致電deleteRowsAtIndexPaths之前,您必須更新數據庫。
  3. 您無需致電beginUpdates/endUpdated即可致電deleteRowsAtIndexPaths

你想:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.Delete { 
     // remove an object from notesItem for this index path 

     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 
    } 
} 
+0

@maddy我得到這個錯誤:終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「無效的更新:行數量無效在部分0中的行數包含在更新(7)之後的現有部分中必須等於更新前(7)中包含在該部分中的行數,加上或減去從該部分插入或刪除的行數(0插入,1刪除)並加上或減去移入或移出該部分的行數(0移入,0移出)。 – coding22

+0

您未更新數據源。將代碼中的註釋替換爲實際代碼以更新數據源。 – rmaddy

+0

我這樣做notesItems.removeObjectAtIndex(indexPath.row),我得到一個錯誤說:NSInternalInconsistencyException',原因:' - [__ NSCFArray removeObjectAtIndex:]:發送到不可變對象的變異方法' – coding22