insertRowsAtIndexPaths插入行a

时间:2014-03-04 12:54:00

标签: objective-c uitableview

我正在尝试在用户按下键盘上的返回后插入一行。 textFieldShouldReturn方法中的代码如下所示:

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:([self.tableView numberOfSections] - 1)] - 1)
                                                inSection:([self.tableView numberOfSections] - 1)];

[self.tableView insertRowsAtIndexPaths:@[lastIndexPath]
                      withRowAnimation:UITableViewRowAnimationRight];

这样可以正常工作,直到显示10行,然后在开始时插入一行,然后在tableView的一端和末尾插入一行。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我很确定您没有更新数据。两个方法中都有一个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewDataSource

会返回错误的数据。

UITableView仅显示数据。你应该把它存放在某个地方。