我尝试在我的UITableView
添加一个由NSFetchedResultsController
管理的额外部分。我希望第一部分,第0部分只包含一行,这很好。但是当从我的核心数据模型和NSFetchedResultsController
填充tableview时,它“认为”它可以使用第一部分来显示数据。我想要的是,它开始填充第1部分的数据,而不是第0部分。
这是我的代码:
#pragma mark UITableView/NSFetchResultsController Helper Methods
- (NSIndexPath *)mapIndexPathFromFetchResultsController:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
return indexPath;
}
- (NSIndexPath *)mapIndexPathToFetchResultsController:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section];
return indexPath;
}
#pragma mark UITableView Delegate & DataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
NSArray *sections = [self.fetchedResultsController sections];
if ( section < [sections count] )
{
//ObjectAtIndex .. How can I trick NSFetchedResultsController to think that section 1 is section 0?
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
if (section == 0) {
numberOfRows++;
}
}
return numberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *normalCellIdentifier = @"DetailTableCell";
static NSString *subMenuCellIdentifier = @"DetailSubMenuTableCell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:subMenuCellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:subMenuCellIdentifier];
}
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
DetailCell *cell = [aTableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
if (!cell) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
}
cell.textLabel.text = @"Test";
return cell;
}
}
SubMenuGroup *group = [self.fetchedResultsController objectAtIndexPath:[self mapIndexPathToFetchResultsController:indexPath]];
for (SubMenus *subMenu in group.menus) {
cell.textLabel.text = subMenu.name;
cell.detailTextLabel.text = subMenu.info;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
return 80;
}
}
return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
if ([[self.fetchedResultsController sections] count] == 0) {
return 1;
}else {
return ([[self.fetchedResultsController sections] count] + 1);
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return 0;
}
return 20;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section) {
NSArray *sections = [self.fetchedResultsController sections];
if ( section < [sections count] )
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
return [[[sectionInfo objects] objectAtIndex:0] name];
}else {
return @"TODO";
}
}
return nil;
}
如何让NSFetchedResultsController
认为第1部分是第0部分,所以第一部分没有添加任何行(但是在行自己的部分中)?
插图,当前如何填充tableview: