将行插入UITableView而不更改先前内容的位置

时间:2012-05-15 13:43:42

标签: objective-c ios uitableview

我正在尝试在表格的开头插入一行,但我想保留以前内容的位置(类似于加载更多推文时的Twitter应用程序),此时我正在使用以下内容代码:

- (void) insertObject: (id) object
{
    [_objects insertObject: object atIndex: 0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}


1 个答案:

答案 0 :(得分:0)

我不确定我完全明白这里的问题。也就是说,如果您只是想在表格的顶部插入一个新行,那么为什么不能只执行以下操作:

- (void) insertObject: (id) object
{
    [_objects insertObject: object atIndex: 0]; // Add the new object to the array (position 0)
    [self.tableView reloadData]; // Reload the data in the table
}

注意:这是基于这样的假设,即在填充表格中的数据时,按顺序遍历数组中的每个对象。