2016-05-15 40 views
0

我正在使用Swift 7和使用MGSwipeTableCell在Xcode 7中的項目。刪除一個項目後更新表:MGSwipeTableCell

當用戶在表格單元格上左/右滑動時,我希望該單元格從視圖中刪除,但不會從我的後端數據庫中刪除,並根據滑動的方向向左/右動畫。當我運行的應用程序和測試功能,它發回這個錯誤:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of 
sections. The number of sections contained in the table view after the update (1) 
must be equal to the number of sections contained in the table view before the 
update (1), plus or minus the number of sections inserted or 
deleted (0 inserted, 1 deleted).' 

這裏是我的功能代碼:

cell.rightButtons = [MGSwipeButton(title: "Skip!", backgroundColor: UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0), callback: { 
      (sender: MGSwipeTableCell!) -> Bool in 
      print("Convenience callback for Skip button!") 

      let indexPath = self.tableView.indexPathForCell(sender) 
      self.tableView.deleteSections(NSIndexSet(index: indexPath!.section), withRowAnimation: UITableViewRowAnimation.Left) 

      return true 
     })] 

     cell.rightExpansion.buttonIndex = 0 

     cell.leftButtons = [MGSwipeButton(title: "Apply!", icon: UIImage(named:"check.png"), backgroundColor: UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0), callback: { 
      (sender: MGSwipeTableCell!) -> Bool in 
      print("Convenience callback for Apply button!") 
      let indexPath = self.tableView.indexPathForCell(sender) 
      self.tableView.deleteSections(NSIndexSet(index: indexPath!.section), withRowAnimation: UITableViewRowAnimation.Right) 

      return true 
     })] 

     cell.leftExpansion.buttonIndex = 0 

     return cell 

,這裏是我的numberOfRowsInSection一塊,我認爲這是導致該問題:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 


    return posts.count 


} 

我是否正確認爲上面的部分需要編輯才能使其工作?我嘗試了幾種不同的方法來解決這個問題,但沒有多少運氣。任何人都可以幫我解決這個問題嗎?

預先感謝您!

多虧了我下面的意見能弄明白

  self.posts.removeAtIndex(indexPath!.row) 

      self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Right) 
+0

錯誤中的問題是*段數*,而不是給定段*中的*行數。這是因爲你調用'deleteSections'來刪除整個部分。 ('numberOfSections'方法的默認實現返回1,所以如果一個部分被刪除了,你需要覆蓋它以返回零)。 –

+0

這是一個我需要添加到VC的additoinal覆蓋函數嗎? – ryanbilak

+0

是的,如果你打算刪除整個部分而不是一行。 –

回答

0

更換deleteSections…tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)。直接在該行之前,從posts刪除帖子。

+0

謝謝!我能夠從上面的評論中找出它,但這是正確的答案。 – ryanbilak

相關問題