Ale*_*lds 5 iphone uitableview
我正在重载委托方法-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;
}
Run Code Online (Sandbox Code Playgroud)
这在第一次绘制表时有效.但是,当我更改设备的方向时,从纵向到横向,单元格高度不会改变.
我尝试-shouldAutorotateToInterfaceOrientation:在表视图控制器中覆盖该方法:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这应该重新加载表数据并且(可能)重绘表格单元格视图.但这没有任何影响.
当设备旋转时,有没有办法强制单元格以新的高度重绘?
编辑:我尝试了以下,没有效果:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
以下是纵向应用程序的外观:

以下是横向应用程序的样子:

内容的顶部和底部边缘之间存在间隙.
编辑2:
通过表格框架的宽度调整尺寸参数有助于解决此显示问题:
CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width,500)];
Run Code Online (Sandbox Code Playgroud)
它不是最优的,但最简单的解决方案就是打电话-reloadData.
一个更好的解决办法是打电话-beginUpdates,-deleteRowsAtIndexPaths:withRowAnimation:,-insertRowsAtIndexPaths:withRowAnimation:,和-endUpdates或者干脆-reloadSections:withRowAnimation:如果只针对3.0.这将添加动画.
编辑:你还需要一个合适的 tableView:heightForRowAtIndexPath:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = [[self textForRowAtIndexPath:indexPath] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)];
return textSize.height + 13.0f;
}
Run Code Online (Sandbox Code Playgroud)
(textForRowAtIndexPath:返回单元格文本的方法在哪里)
| 归档时间: |
|
| 查看次数: |
13419 次 |
| 最近记录: |