从UITableView中移除单元格动画后更新数据源

时间:2015-05-12 13:55:16

标签: ios uitableview swift

我的自定义单元格中有一个删除单元格的按钮。 所以我有一个代表删除它。

视图控制器中的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell =  tableView.dequeueReusableCellWithIdentifier("swipeTableViewCell", forIndexPath: indexPath) as! swipeTableViewCell

    cell.initCell(self, indexPath: indexPath, text: data[indexPath.row])

    return cell
} 

委托方法:

func removeCell(indexPath: NSIndexPath){

    data.removeAtIndex(indexPath.row)
    table.beginUpdates()
    table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    table.endUpdates()
}

单元格中的代码:

func initCell(handler: handleCells, indexPath: NSIndexPath, text: String) {        
    self.handler = handler
    self.indexPath = indexPath
}
按下

按钮:

@IBAction func OnDelButtonClickListener(sender: UIButton) {
    self.handler.removeCell(indexPath)
}

这会移除带动画的单元格,但不会调用reloadData,然后单元格出错indexPath。 因此,当我按下第二个单元格时,删除错误的单元格将被删除。

如果我在table.endUpdates()之后调用reloadData,则没有动画。 如果我打电话

let indexSet = NSIndexSet(index: indexPath.section)
self.table.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.Automatic)

而不是

table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 

我没有删除动画。

有什么建议吗? 感谢

2 个答案:

答案 0 :(得分:2)

row deleting section查看Apple的UITableViews编程指南。

我可能在您的代码中遗漏了一些内容,但看起来您实际上并未删除与已删除单元格对应的数据源中的对象。在删除行之前,请尝试从removeCell函数中删除数据源中的对象。

func removeCell(indexPath: NSIndexPath){

    // here you delete the object form the datasource

    // after that, you do this
    table.beginUpdates()
    table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    table.endUpdates()
}

答案 1 :(得分:0)

我认为关键问题在于当表视图删除单元格时,Cell indexPath无法更新。

所以我们可以尝试在ViewController中创建一个帮助数组,帮助我们更新真正的数据来降低。

lazy var listHelper:Array<Int> = {
var array = [Int]()
for i in 0...self.data.count {
  array.append(i)
}
return array
}()

将removeCell函数更新为:

func removeCell(indexPath: NSIndexPath) {
// if first delete  delete the date, and remove index in help list
if indexPath.row < listHelper.count - 1 && indexPath.row == listHelper[indexPath.row] {
  data.removeAtIndex(indexPath.row)
  tableView.beginUpdates()
  tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
  tableView.endUpdates()
    listHelper.removeAtIndex(indexPath.row)
}else {
  // if indexPath.row  != listHelper[indexPath.row],we find the really data we want to delete, used Array extension  .indexOf

  let locationData = listHelper.indexOf(indexPath.row)
  data.removeAtIndex(locationData!)


  // we create NSIndexPath and delete it.
  let theindexPath = NSIndexPath(forRow: locationData!, inSection: 0)

  tableView.beginUpdates()
  tableView.deleteRowsAtIndexPaths([theindexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
  tableView.endUpdates()
  listHelper.removeAtIndex(locationData!)
}
}

数组扩展:

extension Array {
  func indexOf <U: Equatable> (item: U) -> Int? {
  if item is Element {
  return Swift.find(unsafeBitCast(self, [U].self), item)
  }
  return nil
 }
}

我的英语很差。你可以看到代码。我试过了,它可以工作。我希望可以解决你的问题。