2013-02-26 67 views
1

我試圖將行添加到我的tableView這樣的:ReloadData

[myArray addObject:@"Apple"]; 

[myTableView beginUpdates]; 
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; 
[myTableView endUpdates]; 

的問題是,這些細胞沒有得到刷新。 textLabels不對應於數據源數組。

我加入解決這個問題:

[myTableView reloadData]; 

但是最讓漂亮的插入動畫消失。

想法?謝謝!

+0

作爲@ stephen-darlington表示,當你** addObject:**。它增加了可變數組 – 2013-02-26 08:51:10

回答

4

addObject:將對象添加到結束可變數組的。然後,在下一行中,您告訴表格視圖,在表格的頂部處有一個新行要顯示。除非你的數組「倒退」,否則看起來好像你正在告訴表格在錯誤的地方「插入」一個單元格。

+0

的末尾索引,這是做的伎倆,謝謝! – Michael 2013-02-26 08:59:50

1

你添加項目到數組的結尾,但添加一行到表的開頭...嘗試這樣的:

NSString* obj = @"Apple"; 
[myArray addObject:obj]; 

[myTableView beginUpdates]; 
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[myArray indexOfObject:obj] inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; 
[myTableView endUpdates];