问题描述
我从一个只有一个部分的表视图开始,第一部分总是有三行.下载了一些内容之后,我想在第二部分中显示一个新的单元格,所以我可以这样做:
I start with a tableview with one section, the first section always has three rows. After some content has downloaded, I want show a new cell, in a second section, so I do :
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
但是我得到一个NSInternalConsistencyException:
But I get an NSInternalConsistencyException:
为什么不自动插入新节,如果我确实插入了节,那么在插入不在新节中的行时会遇到问题.
Why isn't it inserted the new section automatically, If I do insert a section, then I get problems when inserting rows that are not in new sections.
为什么不能仅自动添加新部分?当然,InsertRowAtIndexpath:必要时还包括插入一个节,因为NSIndexPath也包括一个节.
Why can't it just add the new section automatically? surely InsertRowAtIndexpath: also include inserting a section if necessary as an NSIndexPath also includes a section.
推荐答案
[self.tableView insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic];
并且不要忘记相应地设置numberOfSectionsInTavleView和numberOfRowsInSection.
and don't forget to set numberOfSectionsInTavleView and numberOfRowsInSection accordingly.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
NSInteger numberOfSections;
if (condition1) {
numberOfSections = 2;
} else if (condition2) {
numberOfSections = 3;
} else {
numberOfSections = 4;
}
return numberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section
if(section == 1) return 3;
else return numberOfRows;
}
这篇关于在新部分的表视图中插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!