自定义 UITableViewController 的标题部分
Posted
技术标签:
【中文标题】自定义 UITableViewController 的标题部分【英文标题】:Customize header section for UITableViewController 【发布时间】: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 【参考方案1】:这个问题比较老,但我想分享我的代码。我正在为我的部分标题使用常用的表格单元格视图。我用接口生成器设计了它并实现了以下委托方法。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"];
cell.textLabel.text = @"test";
return cell;
【讨论】:
为什么要使用单元格作为标题视图?您是否对某种嵌套列表使用两种类型的单元格(标题/正文)?我想知道 sns 应用程序如何实现嵌套列表视图,以及您是否正在做类似的事情? @Eugene 因为您可以在 IB 中设计单元格。是的,我有嵌套列表和至少两个单元格原型“标题”和“项目”。我不确定其他人如何实施这种方法。 这样做的一个有趣的副作用是我刚刚遇到的 - 您似乎在单元格周围获得了彩色边框(即使表格的分隔符设置为无)。手动创建和重新调整 UIView 没有这个问题。注意:我只注意到这一点,因为我有深色标题。 @cocoafan ...viewForHeaderInSection 中没有显示任何内容。 @JayprakashDubey 你是否也将标识符设置为“Header”?【参考方案2】:您可以明确设置委托:
detailViewController.tableView.delegate = detailViewController;
或者你可以在控制器初始函数中进行。
编辑:您的 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;
当然,您也可以在 nib 文件中完成所有这些操作...
【讨论】:
好吧,当然我在这一点上迷路了 :( 我已经尝试了这两种解决方案,但都没有运气。在 ProjectDetails.m 中我已经添加了这个- (id)initWithStyle:(UITableViewStyle)style UITableViewController *tvc = [super initWithStyle:style]; tvc.tableView.delegate = self; return tvc;
,但仍然看不到- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
啊,这就是诀窍...我需要创建自己的 UITableView 而不是使用默认生成的 UITableView。非常感谢!
希望我已经更正了上面的代码错字。感谢您的回答。【参考方案3】:
以下是使用 UITableViewDelegate 方法获取准系统部分视图的方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40.0)];
header.backgroundColor = [UIColor grayColor];
UILabel *textLabel = [[UILabel alloc] initWithFrame:header.frame];
textLabel.text = @"Your Section Title";
textLabel.backgroundColor = [UIColor grayColor];
textLabel.textColor = [UIColor whiteColor];
[header addSubview:textLabel];
return header;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 40.0;
【讨论】:
由于你已经将标签框设置为听者框,所以标题 backgroundColor 指令没有用,因为它被 textLabel backgroundColor one 替换了【参考方案4】:你可以试试这个:在你的 ProjectDetails.h 中声明一个 UIView *tableHeader
和一个访问器方法 - (UIView *)tableHeader;
。然后在实现文件中:
- (UIView *)tableHeader
if (tableHeader)
return tableHeader;
tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
// addlabel
return tableHeader;
在 viewDidLoad 中,调用:self.tableView.tableHeaderView = [self tableHeader];
我认为您不需要使用 heightForHeaderInSection 方法。
【讨论】:
嗨,马克,我已经尝试过了,但是虽然我可以看到该方法被称为部分标题没有改变 :( 我仍然看到我的部分文本来自默认(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
如果您尝试为 tableView 中的多个部分创建自定义标题,那么我可能误解了您的问题。
是的,对于每个部分,我都有一个带有不同文本的标题(也来自数据源)。我将编辑原始问题以明确这一点。以上是关于自定义 UITableViewController 的标题部分的主要内容,如果未能解决你的问题,请参考以下文章
当我从 UITableViewController 打开自定义 UIView 时,如何关闭自定义视图并返回表格视图?
如何从 UiTableViewController 访问自定义 UITableViewCell 的属性?
实现自定义 UITableViewController 类并避免委托警告
容器视图中的自定义 UITableViewController