2016-12-16 76 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()