2012-06-13 104 views
0

我真的想通过使用Interface Builder在XIB中进行布局来完全控制节头。无法从XIB加载viewForSectionHeader

我正在使用下面的代码,但是我的titleLabel.text在我运行应用程序时没有改变。它继续显示每个节标题的默认“标签”文本。我已经NSLog'd headerText的值,它包含正确的字符串。

任何想法?谢谢

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    CustomSectionHeaderViewController *cshvc = [[CustomSectionHeaderViewController alloc] initWithNibName:@"CustomSectionHeaderViewController" bundle:nil]; 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 

    NSString *headerText = [NSString stringWithFormat:@"%@", [sectionInfo name]]; 

    cshvc.titleLabel.text = headerText; 

    return cshvc.view; 
} 

使用答案更新: 下面是我把它读Tammo的回答

CustomSectionHeader *cshv = [[[NSBundle mainBundle] loadNibNamed:@"CustomSectionHeader" owner:self options:nil]objectAtIndex:0]; 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 

NSString *headerText = [NSString stringWithFormat:@"%@", [sectionInfo name]]; 

cshv.textLabel.text = headerText; 


return cshv; 
+0

您是否将'titleLabel'属性连接到Interface Builder中的标题标签? –

+0

是的,它已正确连接 – sayguh

回答

1

声明后的工作:我不认为具有那种视图控制器是正确的事去做。

您的代码存在的问题是控制器仅在需要时加载视图。在访问titleLabel时,视图尚未加载,因此titleLabel为零。尝试

NSString *headerText = [NSString stringWithFormat:@"%@", [sectionInfo name]]; 

// Make sure that the view is loaded 
UIView *result = [cshvc view]; 

cshvc.titleLabel.text = headerText; 

return result;