UITableview插入/删除行动画故障



我有不同的部分和行。每行都有一个可变的高度。我正在通过插入和删除行来扩展和折叠表格。但是当我展开/折叠时,其他单元格相互重叠,导致故障。

func expandItemAtIndex(index:Int) {
  var indexPaths = NSMutableArray()
  var currentSubItems = contentsArray.objectAtIndex(index) as NSArray
  var insertPos = 0
  for(var i = 0; i < currentSubItems.count; i++) {
    indexPaths.addObject(NSIndexPath(forRow: insertPos++, inSection: index))
  }
  self.tableview.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
}
func collapseItemAtIndex(index: Int) {
  var indexPaths = NSMutableArray()
  for (var i = 0; i <  contentsArray.objectAtIndex(index).count; i++) {
    indexPaths.addObject(NSIndexPath(forRow: i, inSection: index))
  }
  self.tableview.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
}
func expandOrCollapseCells(object:AnyObject) {
  self.tableview.beginUpdates()
  if (expandedIndex[index] == index) {
    self.collapseItemAtIndex(expandedIndex[index])
    expandedIndex[index] = -1
  } else {
    expandedIndex[index] = index
    self.expandItemAtIndex(expandedIndex[index])
  }
  self.tableview.endUpdates()
  if (self.tableview.numberOfRowsInSection(index) > 0) {
    self.tableview.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: index), atScrollPosition: UITableViewScrollPosition.Top, animated: true)
  }
}
点击

标题时,我正在呼叫expandOrCollapseCells

你可以在这里找到类似的问题

视频:https://www.youtube.com/watch?v=0YTHu9u88_Y

由于语法差异,我假设您没有使用 Swift 3。

真正突出的一件事是这句话: self.tableview.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)

您是否尝试过改用UITableViewRowAnimation.Top

因为在我看来,视频中可见的"动画"是由beginUpdates((和endUpdates((强制的,因为动画值在实际方法中设置为None

如果您出于某种原因不特别需要扩展的内容成为表格单元格,我也不建议使用另一个单元格来展开/折叠任何给定单元格中的内容。如果不是这种情况,那么我建议在父级单元格结构中布置扩展的内容,并根据需要简单地隐藏/取消隐藏它。

最新更新