2011-08-21 75 views
0

我想知道是否有可能將NSFetchedResultsController與自定義標頭組合?結合NSFetchedResultsController與自定義標頭

這裏是sdandard方式:

- (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]]; 

回答

1

我已經找到一種方法

[textHeader setText:[[[myFetchedResultsController sections] objectAtIndex:section] name]]; 
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]]; 
+0

目標東西TU使用NSFetchedResultsController。我找到了一個方法,如果你有興趣,我會將它發佈。 –