Objective c 将行添加到现有UITableView节

Objective c 将行添加到现有UITableView节,objective-c,cocoa-touch,ios,uitableview,Objective C,Cocoa Touch,Ios,Uitableview,我试图获取一些示例代码,说明如何将行添加到现有的UITableView。我正在尝试使用insertRowsAtIndexPaths:函数 [tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop]; 你知道这些索引是如何工作的吗?我如何添加到现有的节中,或者,如果我没有节,如何创建一个新的节?你必须创建一个索引路径数组,如下所示- NSIndexPath *index

我试图获取一些示例代码,说明如何将行添加到现有的
UITableView
。我正在尝试使用
insertRowsAtIndexPaths:
函数

    [tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop];

你知道这些索引是如何工作的吗?我如何添加到现有的节中,或者,如果我没有节,如何创建一个新的节?

你必须创建一个索引路径数组,如下所示-

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
例如,如果要在节0中插入第2行,并且节0中已有1行,则为节0中的行1创建索引路径,并在表视图上调用insertRowsAtIndexPaths,它将在节中插入第2行

如果表视图中没有节,那么您应该对数据源使用int来表示表视图中没有节,在这种用法中-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return noOfSection; //here section is as int which is initially zero
}

最初您的int“noOfSection”将为零,然后表中将没有节,然后当您要添加节时,将int值增加1并调用
[tableView reloadData]

如果要在表视图中插入行和节,则需要处理这些函数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return noOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[noOfRows objectForIndex:section] intValue];
}
此外,如果要更新表视图,建议使用BeginUpdate和EndUpdate方法

这就是插入新行和节的方式

noOfSections++;
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationTop];


// update your array before calling insertRowsAtIndexPaths: method
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:2]] 
                      withRowAnimation:UITableViewRowAnimationTop];
检查apple提供的示例代码