将行添加到现有的UITableView部分

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

  [tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop]; 

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

你必须创build一个索引path数组,

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

例如,如果要在第0节中插入第二行,并且在第0节中已经有一行,则在第0节中为第1行创build一个索引path,并在表视图中调用insertRowsAtIndexPaths,它将在第二节中插入第二行。

如果在表格视图中没有部分,那么你应该使用一个int数据源在表格视图中给出no部分,在这个使用中 –

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

最初你的int“noOfSection”将为零,那么表中就不会有段了,那么当你想要添加段的时候把你的int值加1并调用[tableView reloadData];

如果要在表格视图中插入行和部分,则需要处理这些function。

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

另外如果你正在更新你的表视图,build议你使用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]; 

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

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