自定义UITableViewController的标题部分

Der*_*tar 6 iphone objective-c uitableview ipad

我需要自定义UITableViewController每个部分的标题部分,返回不同的标题文本(从数据源获取数据).这可以使用以下方法完成:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *temp = [listOfMBeans allKeys];
    DLog(@"MBean details: %@", temp);
    NSString *title = [temp objectAtIndex:section];
    DLog(@"Header Title: %@", title);
    return title;
}; 
Run Code Online (Sandbox Code Playgroud)

这很好用,我可以看到预期的输出.但是我需要更改文本的字体大小,在查看​​类似的问题后,我实现了以下内容:

- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    DLog(@"Custom Header Section Title being set");
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];  

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14];

    [headerView addSubview:label];
    return headerView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}
Run Code Online (Sandbox Code Playgroud)

但是,似乎从未调用过代码.我的理解是UITableViewController默认设置为委托,但似乎我错了.

UITableViewController以这种方式创建(如分层数据的一部分):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProjectDetails *detailViewController = [[ProjectDetails alloc] initWithStyle:UITableViewStyleGrouped];
    detailViewController.project = [listOfMetrics objectAtIndex:indexPath.row];

    // Push the detail view controller.
    [[self navigationController] pushViewController:detailViewController animated:YES];
    [detailViewController release]; 
}
Run Code Online (Sandbox Code Playgroud)

我应该做些什么改变才能使这个工作?谢谢.

coc*_*fan 11

这个问题比较旧,但我想分享我的代码.我正在使用通常的表单元格视图来查看节标题.我用接口构建器设计了它并实现了以下委托方法.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"];
  cell.textLabel.text = @"test";
  return cell;
}
Run Code Online (Sandbox Code Playgroud)

  • @Eugene因为你可以在IB中设计单元格.是的我有嵌套列表和至少两个单元格原型'header'和'item'.我不确定其他人如何实施这种方法. (2认同)

ser*_*gio 3

您可以显式设置委托:

 detailViewController.tableView.delegate = detailViewController;
Run Code Online (Sandbox Code Playgroud)

或者您可以在控制器初始功能中执行此操作。

编辑:您的init方法应该符合规范的 init。此外,在我看来,您还没有创建 UITableView。尝试并使用此代码:

- (id)initWithStyle:(UITableViewStyle)style { 
    if ((self = [super initWithStyle:style])) {
        self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
        self.tableView.autoresizingMask =  UIViewAutoresizingFlexibleWidth  UIViewAutoresizingFlexibleHeight;
        self.tableView.delegate = self;
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

当然,您也可以在 nib 文件中完成所有这些操作......