1

我正在尝试向我的 tableView 添加行,如下所示:

[myArray addObject:@"Apple"];

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

问题是单元格没有被刷新。textLabels 不对应于数据源数组。

我通过添加解决问题:

[myTableView reloadData];

但这会使漂亮的插入动画消失。

想法?谢谢!

4

2 回答 2

4

addObject:将对象添加到可变数组的末尾。然后,在您的下一行,您告诉表格视图有一个新行要显示在表格的最顶部。除非您将数组“向后”,否则看起来好像您是在告诉表格在错误的位置“插入”一个单元格。

于 2013-02-26T08:48:08.840 回答
1

您将项目添加到数组的末尾,但在表的开头添加一行......试试这样:

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

[myTableView beginUpdates];
[myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[myArray indexOfObject:obj] inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
[myTableView endUpdates];
于 2013-02-26T08:46:46.647 回答