【问题标题】:Wrong TableView Height with 64bit Devices64 位设备的 TableView 高度错误
【发布时间】: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 位架构上 CGFloatfloat 类型,而在 64 位架构上是 double。另外,将widthVlaue.integerValue 更改为widthValue.floatValue
  • 现在我正在使用 CGFLoat_MAX,这是我的错误 :) 我将其更改为 widthValue.floatValue 但它没有影响任何内容:/
  • 可以发calculateSizeWithInsets的代码并举报图片吗?

标签: ios objective-c uitableview 32bit-64bit


【解决方案1】:

在我的情况下,正在使用以下方法

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

而不是这个更新的方法。

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

在 64 位架构中,浮点数应替换为 CGFloat

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 2023-03-14
    • 2010-10-23
    • 2014-01-09
    • 2023-04-09
    相关资源
    最近更新 更多