【发布时间】:2009-05-16 13:44:56
【问题描述】:
我正在重载委托方法 -tableView:heightForRowAtIndexPath: 并使用 -sizeWithFont:constrainedToSize: 根据单元格中的文本以编程方式设置单元格的高度:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case(kAboutViewOverviewSection): {
switch (indexPath.row) {
case(kAboutViewOverviewSectionRow): {
NSString *text = NSLocalizedString(@"kAboutViewOverviewSectionFieldText", @"");
CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(280, 500)];
return s.height + 13;
}
default: {
break;
}
}
break;
}
default: {
break;
}
}
return 44.0;
}
这在第一次绘制表格时有效。但是,当我将设备的方向从纵向更改为横向时,单元格高度不会改变。
我尝试在我的表视图控制器中覆盖 -shouldAutorotateToInterfaceOrientation: 方法:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
return YES;
}
这应该重新加载表格数据并(可能)重新绘制表格单元格视图。但这没有任何效果。
当设备旋转时,有没有办法强制单元格以新的高度重绘?
编辑:我尝试了以下方法,但没有效果:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
}
这是应用程序在纵向时的样子:
这是应用程序在横向时的样子:
内容的上下边缘之间存在间隙。
编辑 2:
通过表格框的宽度调整尺寸参数有助于解决此显示问题:
CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width,500)];
【问题讨论】:
标签: iphone uitableview