gpt4 book ai didi

iphone - iPhone 旋转时重置自定义 UITableViewCell 高度

转载 作者:行者123 更新时间:2023-12-03 18:57:04 27 4
gpt4 key购买 nike

我正在重载委托(delegate)方法 -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;
}

这在第一次绘制表格时起作用。但是,当我更改设备的方向(从纵向更改为横向)时,单元格高度不会改变。

我尝试重写 TableView Controller 中的 -shouldAutorotateToInterfaceOrientation: 方法:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
return YES;
}

这应该重新加载表格数据并(可能)重新绘制表格单元格 View 。但这没有任何效果。

当设备旋转时,有没有办法强制单元格重新绘制新的高度?

编辑:我已尝试以下操作,但没有效果:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.tableView reloadData];
}

以下是应用程序在纵向方向上的外观:

Portait

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

Landscape

内容的顶部和底部边缘之间有间隙。

编辑 2:

通过表格框架的宽度调整大小参数有助于解决此显示问题:

CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width,500)];

最佳答案

这不是最佳方案,但最简单的解决方案是在表上调用 -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;
}

(其中 textForRowAtIndexPath: 是返回单元格文本的方法)

关于iphone - iPhone 旋转时重置自定义 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/872461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com