【发布时间】:2011-05-31 13:45:53
【问题描述】:
我需要自定义 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;
};
这很好用,我可以看到预期的输出。但是,我还需要更改文本的字体大小,在查看了类似问题后,我实现了以下内容:
- (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;
}
但似乎从未调用过代码。我的理解是 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];
}
我应该进行哪些更改才能使其正常工作? 谢谢。
【问题讨论】:
-
我不太清楚,你要什么?
-
确保你设置了新的表格视图委托,也许在 'projectDetails' 的 init 方法中。
-
@rptwsthi 基本上如何让我的 UITableViewController ProjectDetails 调用 viewForHeaderInSection 以自定义标题视图
-
检查方法名的大小写...是tableView,不是tableview
标签: iphone objective-c ipad uitableview