ios 将行添加到现有 UITableView 部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5906683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add rows to existing UITableView section
提问by Scoota P
I am trying to get some sample code on how I would add rows to an existing UITableView
. I am trying to use the insertRowsAtIndexPaths:
function.
我正在尝试获取一些关于如何将行添加到现有UITableView
. 我正在尝试使用该insertRowsAtIndexPaths:
功能。
[tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop];
Any ideas how these indexes work and how I can add to an existing section or, if I don't have a section, then create a new section?
任何想法这些索引如何工作以及我如何添加到现有部分,或者如果我没有一个部分,然后创建一个新部分?
回答by saadnib
You have to create an array of indexpaths as -
您必须创建一个索引路径数组作为 -
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
for example if you want to insert 2nd row in section 0 and already have 1 row in section 0 then create an index path for row 1 in section 0 and call the insertRowsAtIndexPaths on table view, it will insert 2nd row in section.
例如,如果要插入第二排在第0和已经有1列在第0然后创建在第0第1行的索引路径,并呼吁表格视图insertRowsAtIndexPaths,将在部分插入第二行。
If there is no section in table view then you should use an int for data source to give no of sections in table view, in this use -
如果表视图中没有节,则应使用 int 作为数据源以在表视图中不提供节,在此使用中 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return noOfSection; //here section is as int which is initially zero
}
and initially your int "noOfSection" will be zero then there will be no section in table, then when you want to add section increase your int value by 1 and call [tableView reloadData];
并且最初您的 int "noOfSection" 将为零,然后表中将没有任何部分,然后当您想添加部分时,将您的 int 值增加 1 并调用 [tableView reloadData];
回答by Robin
You need to handle these functions if you are gonna insert row and section in a table view.
如果要在表视图中插入行和节,则需要处理这些函数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return noOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[noOfRows objectForIndex:section] intValue];
}
also if you are updating your table view it is recommended that you use beginUpdates and endUpdates method.
此外,如果您要更新表视图,建议您使用 beginUpdates 和 endUpdates 方法。
this is how you insert new row and section.
这就是插入新行和节的方式。
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];
check the sample code provided by the apple.
检查苹果提供的示例代码。