将行添加到现有UITableView部分

Sco*_*a P 8 cocoa-touch objective-c uitableview ios

我试图获得一些关于如何向现有行添加行的示例代码UITableView.我正在尝试使用该insertRowsAtIndexPaths:功能.

    [tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop];
Run Code Online (Sandbox Code Playgroud)

任何想法这些索引如何工作以及如何添加到现有部分,或者,如果我没有部分,然后创建一个新部分?

saa*_*nib 11

您必须创建一个索引路径数组 -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
Run Code Online (Sandbox Code Playgroud)

例如,如果要在第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
}
Run Code Online (Sandbox Code Playgroud)

并且最初你的int"noOfSection"将为零,那么表格中没有任何部分,那么当你想要添加部分时,将你的int值增加1并调用 [tableView reloadData];


Rob*_*bin 6

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return noOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[noOfRows objectForIndex:section] intValue];
}
Run Code Online (Sandbox Code Playgroud)

另外,如果要更新表视图,建议您使用beginUpdates和endUpdates方法.

这是您插入新行和节的方式.

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];
Run Code Online (Sandbox Code Playgroud)

检查苹果提供的示例代码.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html