Combine NSFetchedResultsController with custom header
I would like to know if it's possible to combine the NSFetchedResultsController with custom header ?
Here is the sdandard way :
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [myFetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
Here is what I'm trying to set up
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 23)] autorelease];
//set the background
UIImageView* TopBarHeader = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 23)] autorelease];
[TopBarHeader setImage:[UIImage imageNamed:@"barre_separation_bg.png"]];
//set the shadow
[[TopBarHeader layer] setShadowColor:[[UIColor blackColor] CGColor]];
[[TopBarHeader layer] setShadowOffset:CGSizeMake(1.0f, 3.0f)];
[[TopBarHeader layer] setShadowOpacity:0.5f];
[[TopBarHeader layer] setShadowRadius:1.0f];
[headerView addSubview:TopBarHeader];
//set the text
UILabel *textHeader = [[[UILabel alloc] initWithFrame:CGRectMake(11, 0, 320, 20)] autorelease];
[textHeader setText:[myFetchedResultsController sectionForSectionIndexTitle:title atIndex:index]];
[textHeader setTextColor:[UIColor colorWithRed:(124/255.0) green:(132/255.0) blue:(137/255.0) alpha:1]];
[textHeader setBa开发者_StackOverflow社区ckgroundColor:[UIColor clearColor]];
[headerView addSubview:textHeader];
return headerView;
}
How can I make work this line ?
[textHeader setText:[myFetchedResultsController sectionForSectionIndexTitle:title atIndex:index]];
I have found a way
[textHeader setText:[[[myFetchedResultsController sections] objectAtIndex:section] name]];
I think you are heading in the wrong direction. sectionForSectionIndexTitle:
is used to return the section number corresponding to the title and index, whereas, in your case you require the section header corresponding to an index. You don't need to ask the NSFetchedResultsController. You can simply add a method in your UITableViewController
sub-class. Something like-
-(NSString*) sectionHeaderForIndex: (NSInteger)section
{
switch(section)
{
case 0:
return @"name of section 0";
case 1:
return @"name of section 1";
//and so on...
}
assert(NO);
return nil;
}
And then,
[textHeader setText:[self sectionHeaderForIndex:section]];
精彩评论