64位设备上TableView高度不正确

3

我有一个非常奇怪的问题。在64位设备上,我总是得到错误的单元格高度。在32位设备上,它看起来像这样:

enter image description here

在64位架构的设备上,它看起来像这样:

enter image description here

这是我的函数,是的,在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 - Guy Kogus
你遇到的困难可能与32位架构下的CGFloat类型为float有关,而在64位架构下则为double。此外,请将widthVlaue.integerValue更改为widthValue.floatValue - Guy Kogus
现在我正在使用CGFloat_MAX,那是我的错误 :) 我把它改成了widthValue.floatValue,但它没有影响任何东西 :/ - Davis
你能发布 calculateSizeWithInsets 的代码并报告图片吗? - Sash Zats
1个回答

0
在我的情况下,以下方法被使用。
- (float)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath

使用这个更新的方法代替。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

在64位架构中,float应该被替换为CGFloat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接