UITableView无法及时滚动以查看插入单元格动画

时间:2016-12-16 03:26:31

标签: ios uitableview

我有一个UITableView单元格。行数通常为6,但是当某个事件被触发时,这将增加到7,我想在底部插入一个带动画的新行。目前我的代码是这样的:

(数据源已更新)

tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row:6, section: 0)], with: .left)
tableView.endUpdates()
tableView.scrollToRow(at: IndexPath(row:6, section: 0), at: .top, animated: true)

这会将行和滚动插入到底部,但我无法正确查看单元格动画。它更像是插入单元格然后滚动,所以我没有看到它从左边动画。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

此代码用于从左侧动画的表视图中删除一行。您可以尝试添加行,也可以在主线程中更新表视图。

 tableview.beginUpdates()
    var indexPathsToDeleteForAnimation: [NSIndexPath] = []
    var numOfCellsToRemove = ArrayOfItemsToRemove ?? 0

    // Do your work here
    while numOfCellsToRemove > 0 {
        // ...or here, if you need to add/remove the same amount of objects to/from somewhere
        indexPathsToDeleteForAnimation.append(NSIndexPath(forRow: selectedCellIndex+numOfCellsToRemove, inSection: 0))
        numOfCellsToRemove -= 1
    }
    tableview.deleteRowsAtIndexPaths(indexPathsToDeleteForAnimation, withRowAnimation: UITableViewRowAnimation.Right)
    tableview.endUpdates()