我的问题基本上归结为在UITableCell中支持UILabel的动态高度(我想其他元素)的最佳方法,并且在旋转时也正确地调整标签宽度/高度和单元格高度.
我知道如何获得UILabels的预期高度,并相应地调整高度,但是当你支持横向时,事情似乎也变得非常棘手.
我认为这可能是layoutSubviews的路线,但我不确定如何将它与需要计算单元格高度的表结合起来.另一篇有趣的帖子在init上手动设置框架,以确保它们在已知数量的情况下进行计算,但这只能解决部分问题.
所以这是我正在处理的图像,红色箭头指向动态高度标签,蓝色箭头指向将改变高度的单元格.

我已经设法让它正常工作,但不确定它是否是正确的方法.
cellForRowAtIndexPath始终以纵向模式显示其大小.(即,对于iPhone应用程序,它总是报告320).cellForRowAtIndexPath正确的方向给出其大小[tableView reloadData]轮换.我找不到任何其他方法来更新单元格和标签高度在viewDidLoad我获取对每个标签字体的引用,以及与纵向中的tableView相比标签宽度百分比的浮点数
CWProductDetailDescriptionCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"DescriptionCell"];
self.descriptionLabelWidthPercentage = cell.descriptionLabel.frame.size.width / 320;
self.descriptionLabelFont = cell.descriptionLabel.font;
Run Code Online (Sandbox Code Playgroud)在heightForRowAtIndexPath我使用tableView宽度和我已经抓住的百分比计算单元格高度:
case TableSectionDescription:
CGFloat labelWidth = self.tableView.frame.size.width * self.descriptionLabelWidthPercentage;
CGSize newLabelFrameSize = [self sizeForString:self.product.descriptionText WithConstraint:CGSizeMake(labelWidth, MAXFLOAT) AndFont:self.descriptionLabelFont];
return newLabelFrameSize.height + kTextCellPadding;
Run Code Online (Sandbox Code Playgroud)在cellForRowAtIndexPath我计算标签框架的框架并更新它
cell = [tableView dequeueReusableCellWithIdentifier:@"DescriptionCell"];
((CWProductDetailDescriptionCell *)cell).descriptionLabel.text = self.product.descriptionText;
CGRect oldFrame = ((CWProductDetailDescriptionCell *)cell).descriptionLabel.frame;
CGFloat labelWidth = self.tableView.frame.size.width * self.descriptionLabelWidthPercentage;
CGSize newLabelFrameSize = [self sizeForString:self.product.descriptionText WithConstraint:CGSizeMake(labelWidth, MAXFLOAT) AndFont:((CWProductDetailDescriptionCell *)cell).descriptionLabel.font];
((CWProductDetailDescriptionCell *)cell).descriptionLabel.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, labelWidth, newLabelFrameSize.height);
Run Code Online (Sandbox Code Playgroud)在didRotateFromInterfaceOrientation你需要[self.tableView reloadData]
所以.这样做的正确方法是什么?我已经看过很多SO问题,但没有一个完全解决这个问题.
Bob*_*ryn 12
显然,它写出了我的巨大问题,并从计算机中休息了解.
heightForRowAtIndexPath
cellForRowAtIndexPath除初始设置外,
不要进行任何帧调整willDisplayCell forRowAtIndexPath
请注意,这些是保留/强(ARC),并且从不添加到表本身
self.descriptionReferenceCell = [self.tableView dequeueReusableCellWithIdentifier:@"DescriptionCell"];
self.attributeReferenceCell = [self.tableView dequeueReusableCellWithIdentifier:@"AttributeCell"];
Run Code Online (Sandbox Code Playgroud)
heightForRowAtIndexPath计算可变高度单元格我首先更新我的参考单元格的宽度,以便它显示其子视图(UILabels),然后我可以将其用于计算.然后我使用更新的标签宽度来确定标签的高度,并添加任何必要的单元格填充来计算我的单元格高度.(请记住,只有当您的细胞与标签一起改变高度时,才需要进行这些计算).
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
float width = UIDeviceOrientationIsPortrait(orientation) ? 320 : 480;
case TableSectionDescription:
CGRect oldFrame = self.descriptionReferenceCell.frame;
self.descriptionReferenceCell.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, width, oldFrame.size.height);
CGFloat referenceWidth = self.descriptionReferenceCell.descriptionLabel.frame.size.width;
CGSize newLabelFrameSize = [self sizeForString:self.product.descriptionText WithConstraint:CGSizeMake(referenceWidth, MAXFLOAT) AndFont:self.descriptionReferenceCell.descriptionLabel.font];
return newLabelFrameSize.height + kTextCellPadding;
Run Code Online (Sandbox Code Playgroud)
cellForRowAtIndexPath不要依赖方向做任何动态布局相关更新willDisplayCell forRowAtIndexPath更新标签高度本身由于表格单元格已经执行layoutSubviews,标签已经具有正确的宽度,我用它来计算标签的确切高度.我可以安全地更新我的标签高度.
case TableSectionDescription:
CGRect oldFrame = ((CWProductDetailDescriptionCell *)cell).descriptionLabel.frame;
CGSize newLabelFrameSize = [self sizeForString:self.product.descriptionText WithConstraint:CGSizeMake(oldFrame.size.width, MAXFLOAT) AndFont:((CWProductDetailDescriptionCell *)cell).descriptionLabel.font];
((CWProductDetailDescriptionCell *)cell).descriptionLabel.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, newLabelFrameSize.height);
break;
Run Code Online (Sandbox Code Playgroud)
如果不重新加载数据,则可见单元格不会更新其高度和标签.
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
唯一需要指出的是,我正在使用帮助程序类来计算仅包装基本调用的标签高度.真的,它不再保存任何代码(我曾经有其他东西),所以只需直接使用正常的方法你的字符串.
- (CGSize) sizeForString:(NSString*)string WithConstraint:(CGSize)constraint AndFont:(UIFont *)font {
CGSize labelSize = [string sizeWithFont:font
constrainedToSize:constraint
lineBreakMode:UILineBreakModeWordWrap];
return labelSize;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15260 次 |
| 最近记录: |