我正在重载委托方法-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)];
UITableView
已更改,需要在返回单元格之前知道单元格的大小。我已更新示例以依赖新的textForRowAtIndexPath:
方法,该方法仅返回行的文本。 - rpetrich