我正在尝试将我的一个应用程序的代码更新到iOS7,并将其转换为使用Storyboards。在尝试使表视图标题显示多行文本时遇到了问题(表视图单元格仍然正常工作)。
TableView是使用Storyboard创建的,但我正在使用旧程序中的自定义单元格而不是在Storyboard中构建新的自定义单元格。
我之前运行良好的代码:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section)
{
case 0:
return [NSString stringWithFormat:@"Fixture Counts per \nTable 2902.1 - %@ IBC\n\n Occupancy Recap", strCodeYear ]; //(rounded up)
break;
case 1:
return [NSString stringWithFormat:@"Womens Fixtures"];
break;
case 2:
return [NSString stringWithFormat:@"Mens Fixtures"];
break;
case 3:
return [NSString stringWithFormat:@"General Fixtures"];
break;
}
// To avoid "control reaches end of non-void function"
return 0;
}
\n以前足以使标题根据文本行扩展。我可以看出\n仍然会引发换行,但只有一行文本可见。
我已调整标题大小以确保有足够的空间,但仍然只有一行文本:
// Set Custom Heights for Headers
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 0 )
return 200.0f;
else return 60.0f; // put 22 in case of plain one..
}
我试图找到一种像对待标签一样使用 numberOfLines
的方法,但没有成功。我查阅了 UITableView 类参考文档,但没有找到与行数相关的内容,不过还是尝试了一下:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 2;
return 0; // nil 0 view section
}
非常感谢您的帮助。
tableView:viewForHeaderInSection:
方法没有给出任何警告,却仍然能够获取到文本。你应该返回一个视图,而不是返回nil
。 - nhgrif