2011-05-06 90 views
8

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

[tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop]; 

任何想法,这些指标是如何工作的,我怎么能添加到现有的部分,或者,如果我没有一节,然后创建一个新的部分?

回答

11

你必须创建indexpaths数组作为 - 如果你想在一节0插入第2行已经在部分0 1行,然后创建第1行的索引路径

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

例如第0节并在表视图上调用insertRowsAtIndexPaths,它将在第2节中插入节。

如果在表视图无节,那么你应该使用int数据源不给他们部分的表视图,在此使用 -

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

,最初你诠释“noOfSection”将零然后表中将没有部分,然后当你想添加部分增加你的int值1和[tableView reloadData];

6

如果你要在表视图中插入行和部分,你需要处理这些函数。

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

此外,如果您正在更新您的表视图,建议您使用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