2016-12-16 71 views
0

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

(数据源已被更新)

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) 

这就将行和滚动至底部,但我不能正确地看到细胞动画。它更像是插入单元格然后滚动,因此我没有看到它从左侧开始动画。我在这里做错了什么?

回答

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()