删除行UITableView索引问题

我正在使用下面的代码来删除我的tableview中的一行。 首先,我从我的数组中删除对象,然后从tableview中使用下面的代码:

let i = IndexPath(item: rowNum, section: 0) myArray.remove(at: rowNum) myTableView.deleteRows(at: [i], with: UITableViewRowAnimation.left) 

但是,如果我之后删除另一行,而不是我想要删除的行被删除。 问题是,即使我删除了tableview中的第一项(例如索引0),单击新的第一行返回索引1 …这是错误的,并删除第二行。 删除第一行后,顶部的新行应该有一个索引0。

我可以通过这样做来解决这个问题:

 mTableView.reloadData() 

但这似乎是错误的…我不应该重新加载所有的数据了。

我还可以做些什么?

编辑:我有我的tableviewcell中的自定义button,我正在按删除行 – 不刷卡。

问题

您遇到的问题是您在button上设置的标签,button标签在删除行时不会更改,因为其所有单元格都不是重新加载的。 我想你是在cellforRowIndexpath设置标签,你试着把标记放在tableWillDisplayCell

要么

最好的办法是在下面。

您可以通过其他方式获得IndexPath

假设你的视图层次是

UITableViewCell-> contentView – >button

在你button点击的方法

  if let cell = sender.superview?.superview as? UITableviewCell{ let indexPath = myTableView.indexPath(for: cell) // Now delete the table cell myArray.remove(at: indexPath.row) myTableView.beginUpdates() myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left) myTableView.endUpdates() } 

我希望这会为你工作。

尝试这个!

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { //1. remove data from model data.remove(at: indexPath.row) //2. remove row from view tableView.deleteRows(at: [indexPath as IndexPath], with: .fade) } } 

智能解决scheme,当在相应的单元格中按下button时删除一行。

  • 在自定义单元类中声明一个callbackvariables

     var callback : (()->())? 
  • 在自定义单元类中实现一个IBAction并将该button连接到该动作

     @IBAction func buttonPressed(_ sender : UIButton) { callback?() } 
  • cellForRowAtIndexPath分配包含代码的闭包以删除单元格

     cell.callback = { self.myArray.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .left) } 

我认为你存储rowNumvariables与您的单元格,它设置在cellForRow(at:)方法。 如果我认为是对的,那么这是一件事情。

UITableView尽可能至less做一些工作。 这意味着在你删除单元格之后, UITableView将不会在数据源上再次调用cellForRow(at:)方法,直到调用reloadData()reloadRows(at:animation:)方法。

所以,当你删除第一个单元格时,其他单元格上的rowNumvariables将不会被更新,直到你尝试调用reloadData() ,并看到这是工作。

我的build议是不要保持rowNumvariables在一个单元格,而是通过UITableView indexPath(for:)方法,而不是在UITableView方法,而是要求UITableView的任何单元格的索引path。

使用苹果的代码

表视图的行为与在更新块中调用的重新装入方法的行为相同 – 在执行animation块之前,将重新加载行和段的索引。 无论插入,删除和重新加载方法调用的顺序如何,都会发生此问题。

 [tv beginUpdates]; [tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; [tv endUpdates]; 

删除该行后,重新加载特定的行

 let indexPath = IndexPath(item: rowNum, section: 0) array.remove(at: rowNum) tableView.beginUpdates() myTableView.deleteRows(at: [i], with: UITableViewRowAnimation.automatic) tableView.endUpdates() self.tableView.reloadRows(at: [indexPath], with: .automatic) 

我在我的应用程序中使用下面的代码:

 public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){ if editingStyle == UITableViewCellEditingStyle.delete { myArray.remove(at: indexPath.row) myArray2.remove(at: indexPath.row) // you would also save the new array here so that the next time you open the tableview viewController it shows the changes made. mytableview.reloadData() } } 

我不知道从哪里得到你的var rowNum ,但对我来说,这个问题看起来像它已经发生在我身上的东西。

对我来说,问题是传递给单元格indexPath tableView: cellForRowAt indexPath:中的tableView: cellForRowAt indexPath:导致此值在单元被删除时未被更新。

换句话说,在删除的单元格的button之后排列的所有button必须将其索引移动-1,否则它们将删除第n + 1行。

 row A index 0 row B index 1 row C index 2 row D index 3 

删除B它发生

 row A index 0 row C index 2 row D index 3 

而C应该有索引1,D应该有索引2.在这种情况下点击button删除C会导致D被删除,而点击button删除D会导致崩溃。

我希望我已经清楚了。

如果您想从自定义button操作中删除tableview行,除了tableview commit editingStyle委托或任何tableview委托方法,您必须调用tableview reloadData来填充更新后的索引值。

任何插入或删除行的方法都必须在tableView.beginUpdates()tableView.endUpdates()调用。 所以你的代码必须是:

 let i = IndexPath(item: rowNum, section: 0) myArray.remove(at: rowNum) tableView.beginUpdates() myTableView.deleteRows(at: [i], with: UITableViewRowAnimation.left) tableView.endUpdates()