2016-09-22 63 views
0

我有一個簡單的表視圖,其中單元格有一個數字標籤,並且它們根據其行號進行編號。這一切工作正常,直到我試圖插入一個新的行頂部插入新行時的更新表視圖編號

我需要一種方法來更新下面的所有單元格,以便編號是正確的。重新加載tableview的作品,但它切斷了行插入動畫。任何人都知道這個解決方案?

謝謝!

+0

使用'indexPathsForVisibleRows'來檢索可見的行,然後可以遍歷返回的數組,使用'cellForRowAtIndexPath'來檢索相應的單元格並更新其標籤中的行號。你也可以嘗試'tableView.reloadRowsAtIndexPaths(tableView.indexPathsForVisibleRows(),withRowAnimation:.None)',但這也可能會中斷動畫 – Paulw11

回答

0

,如果你設置了正確的cellForRowAt指數函數,其中每個標籤數量從indexpath.row然後導出之後再插入發生就叫

tableView.reloadData() 
+0

正如我已經提到的那樣,它會切斷動畫。 –

+0

哎呀抱歉,沒有看到。是的,我之前也遇到過這個問題,您可以嘗試使用reloadSections方法,但我認爲我從來沒有碰運氣。所以相反,我認爲我最終設置了一個計時器,在插入動畫(可能是0.3秒)後關閉,然後重新加載表視圖。不漂亮,但它的工作 – MobileMon

0

您可能需要使用該解決方案雨燕3.0

CATransaction.begin() 
tableView.beginUpdates() 

CATransaction.setCompletionBlock {() -> Void in 
    // this is supposed to be done after insertion animation  
    self.tableView.reloadSections(IndexSet([0]), with: UITableViewRowAnimation.automatic) 
} 
self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic) 

tableView.endUpdates() 
CATransaction.commit() 
0

刷新你的錶行插入完成後,如工作圍繞下面的代碼可以幫助:

[CATransaction begin]; 

[tableView beginUpdates]; 


[CATransaction setCompletionBlock: ^{ 

    //Reload your tableView here 

}]; 

[tableView insertRowsAtIndexPaths: indexPaths 
       withRowAnimation: UITableViewRowAnimationAutomatic]; 


[tableView endUpdates]; 

[CATransaction commit];