【发布时间】:2014-07-07 07:43:07
【问题描述】:
我有一个非常奇怪的问题。对于 64 位设备,我总是得到错误的单元格高度。在 32 位设备上,它看起来正确,如下所示:
在 64 位架构的设备上,它看起来像这样:
这是我的功能,是的,在 64 位设备发布后,我将其从 float 更改为 CGFloat....
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
GFBlockTableRowItem* row = [self.blockTable.tableRows objectAtIndex:indexPath.row];
CGFloat maxHeight = 0.0f;
for (NSUInteger i=0; i<row.tableColumns.count; ++i) {
NSString* widthValue = [self.blockTable.tableColWidths objectAtIndex:i];
CGFloat percentage = widthValue.integerValue/100.0f;
NSString* string = [row.tableColumns objectAtIndex:i];
CGSize constraint = CGSizeMake(self.frame.size.width*percentage, FLT_MAX);
CGSize size = calculateSizeWithInsets(string, self.defaultCellFont, constraint, GTDefaultTableEdgeInsets);
maxHeight = MAX(size.height, maxHeight);
}
LogDebug(@"height %li: %.3f", (long)indexPath.row, maxHeight);
return maxHeight;
}
编辑 1:
typedef NS_ENUM(NSInteger, GTVerticalTextAlignment) {
GTVerticalTextAlignmentTop,
GTVerticalTextAlignmentCenter,
GTVerticalTextAlignmentBottom
};
/**top, left, bottom, right*/
static UIEdgeInsets GTDefaultTableEdgeInsets = {0.0f, 0.0f, 0.0f, 0.0f};
static UIEdgeInsets GTDefaultTextViewEdgeInsets = {8.0f, 4.0f, 8.0f, 4.0f};
CGSize calculateSizeWithInsets(NSString *text,UIFont *font, CGSize size, UIEdgeInsets insets) {
// GT_SIZE_CONSTRAINT_NONE
CGSize constraint = size;
UIEdgeInsets effective = UIEdgeInsetsMake((insets.top + GTDefaultTextViewEdgeInsets.top),
(GTDefaultTextViewEdgeInsets.left + insets.left),
(insets.bottom + GTDefaultTextViewEdgeInsets.bottom),
(GTDefaultTextViewEdgeInsets.right + insets.right));
if (constraint.width != GT_SIZE_CONSTRAINT_NONE) {
constraint.width = constraint.width - (effective.left + effective.right);
}
if (constraint.height != GT_SIZE_CONSTRAINT_NONE) {
constraint.height = constraint.height - (effective.top + effective.bottom);
}
CGSize textSize = calculateSize(text, font, constraint);
if (constraint.width == GT_SIZE_CONSTRAINT_NONE) {
textSize.width += effective.left + effective.right;
}
if (constraint.height == GT_SIZE_CONSTRAINT_NONE) {
textSize.height += effective.top + effective.bottom;
}
return textSize;
}
【问题讨论】:
-
你为什么用
FLT_MAX而不是CGFLOAT_MAX? -
您遇到的困难可能与以下事实有关:在 32 位架构上
CGFloat是float类型,而在 64 位架构上是double。另外,将widthVlaue.integerValue更改为widthValue.floatValue。 -
现在我正在使用 CGFLoat_MAX,这是我的错误 :) 我将其更改为 widthValue.floatValue 但它没有影响任何内容:/
-
可以发
calculateSizeWithInsets的代码并举报图片吗?
标签: ios objective-c uitableview 32bit-64bit