2014-09-05 166 views
1

我有一張桌子,可能有幾百行,都有不同的高度。每當用戶刪除一行,呼叫:TableView deleteRowsAtIndexPaths:withRowAnimation非常慢

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

需要很長時間才能執行(大約2.5s)。

有沒有其他的電話或這種方式來加速?謝謝!

+1

這個方法也會調用一些數據源委託的方法,我想這就是它被卡住的地方,你能告訴你如何實現'UITableView'的數據源委託嗎? – KudoCC 2014-09-05 09:41:04

+1

一定要在主線程上調用它 – Andrea 2014-09-05 09:43:31

回答

1

使用UITableView的方法estimatedRowHeight加快速度。

提供行高度的非負估計可以提高加載表視圖的性能。如果表格包含可變高度的行,則在表格加載時計算其所有高度可能會很昂貴。使用估算功能可以將幾何計算的成本從加載時間推遲到滾動時間。

在任何情況下,您都應該使用Instruments來測量哪些代碼部分佔用了大部分時間。

+1

到目前爲止,這似乎是最好的選擇。它確實減少了時間,但很少有不良影響。它現在正在破壞約束條件,它在表格頂部比底部更快(我猜測它必須加載它上面的所有高度)。謝謝! – Diesel 2014-09-06 15:22:47

+2

編輯:通過添加更精確的高度來修正約束問題。還修復了我的錶慢慢加載。問題仍然存在於桌子底部,但速度要快得多(0.5s而不是2.5s) – Diesel 2014-09-06 15:37:07

1

你可以試試這個。它有點不道德,但它像一個魅力。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
if([textArray count]>0){ 
    [self deleteRowAtIndexPath:indexPath.row]; 
} //method fired when u select a row, which calls a method 
} 

-(void)deleteRowAtIndexPath:(int) indexPath{ //this method removes the data in the array from which the tableview is being populated 
[textArray removeObjectAtIndex:indexPath]; 
[self.tableView reloadData]; //after deleting the data from the array it reloads the tableview 
} 

textArray是一個NSMutableArray,從中填充tableview。

但是這個dsnt有任何動畫。