将NSFetchedResultsController与自定义标头组合在一起

时间:2011-08-21 20:26:35

标签: iphone ios header nsfetchedresultscontroller

我想知道是否可以将NSFetchedResultsController与自定义标头结合使用?

这是标准方式:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [myFetchedResultsController sectionForSectionIndexTitle:title atIndex:index];

以下是我要设置的内容

- (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 setBackgroundColor:[UIColor clearColor]];
    [headerView addSubview:textHeader];

    return headerView;
}

我该如何开展这项工作?

  [textHeader setText:[myFetchedResultsController sectionForSectionIndexTitle:title atIndex:index]];

2 个答案:

答案 0 :(得分:1)

我认为你正走向错误的方向。 sectionForSectionIndexTitle:用于返回与标题和索引对应的节号,而在您的情况下,您需要与索引对应的节标题。您不需要询问NSFetchedResultsController。您只需在UITableViewController子类中添加方法即可。像 -

这样的东西
-(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;
}

然后,

[textHeader setText:[self sectionHeaderForIndex:section]];

答案 1 :(得分:1)

我找到了一种方法

[textHeader setText:[[[myFetchedResultsController sections] objectAtIndex:section] name]];