0

Cell 中的 UILabel 包含一个 NSLocalizedString;根据语言的不同,从几个字符到一个段落不等。UILabel 的框架有时会,有时不会绘制以适应字符串长度。我相信这是重用的问题,但我不知道如何解决它。

我从 Nibs(项目要求)加载了一系列五个不同的 UITableViewCell 子类viewDidLoad

UINib *Cell1Nib = [UINib nibWithNibName:@"customCell1" bundle:nil];
[[self tableView] registerNib:Cell1Nib forCellReuseIdentifier:@"custCell1"];

UINib *Cell2Nib = [UINib nibWithNibName:@"customCell2" bundle:nil];
[[self tableView] registerNib:Cell2Nib forCellReuseIdentifier:@"custCell2"];
//etc.

我有一个助手可以根据使用的文本获取 UILabel 的高度:

- (CGFloat)getTextHeight:(NSString *)locStr forLabelWidth:(CGFloat)w {
    UILabel  *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 110)];
    label.numberOfLines=0;
    label.lineBreakMode=NSLineBreakByCharWrapping;
    label.text = locStr; // highly variable & already localized
    label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 
    CGSize maxSize = CGSizeMake(w, CGFLOAT_MAX);
    CGSize requiredSize = [label sizeThatFits:maxSize];
    label = nil; // ARC paranoia
    return requiredSize.height;
}

heightForRowAtIndexPath我调用getTextHeight:forLabelWidth:中,使用它来确定单元格高度并将结果存储到一个名为的 NSMutableArray 中,labelHeightforRow.到目前为止,一切正常。

我这样创建表:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    DLX_CellDescriptor *desc = [[DLX_CellDescriptor alloc] initWithRow:indexPath.row];
    //DLX_CellDescriptor describes attributes for cell at index , essentially a list of strings
    CGFloat helpTextHeight = [[labelHeightforRow objectAtIndex:indexPath.row] floatValue];

    if ([desc.nibToUse caseInsensitiveCompare:@"custCell1"] == NSOrderedSame ) {
        DLX_CustCell1 *currCell = (DLX_CustCell1 *)[self.tableView dequeueReusableCellWithIdentifier:@"custCell1"];
        CGPoint origin = currCell.theLabel.frame.origin;
        CGFloat w = currCell.theLabel.frame.size.width;
        currCell.theLabel.frame = CGRectMake(origin.x, origin.y, w, helpTextHeight);
        currCell.theLabel.text = desc.localizedText;
        currCell.theLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        currCell.tag = indexPath.row;
        cell = currCell;
    } else if ([desc.nibToUse caseInsensitiveCompare:@"custCell2"] == NSOrderedSame ) {
        DLX_CustCell2 *currCell = (DLX_CustCell2 *)[self.tableView dequeueReusableCellWithIdentifier:@"custCell2"];
        CGPoint origin = currCell.theLabel.frame.origin;
        CGFloat w = currCell.theLabel.frame.size.width;
        currCell.theLabel.frame = CGRectMake(origin.x, origin.y, w, helpTextHeight);
        currCell.theLabel.text = desc.localizedText;
        currCell.theLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        currCell.tag = indexPath.row;
        cell = currCell;
    } // for about 26 rows of 5 different non-identical sytles
      // simplified in this example
    return cell;
}

这个问题是在创建初始TableView时,UILabel是Nib中指定的高度:截断超长文本并在下方给出大的空白(来自正确的单元格高度调整)。重绘表格时,UILabel 变为代码中指定的正确高度;显示整个字符串。然后,如果重新创建单元格;比如说,在它滚出屏幕并绘制了另一个单元格类型之后,长文本标签再次使用从笔尖截断的高度。

我尝试layoutSubviews像这样放入自定义 UITableViewCell(例如 customCell1.m):

- (void)layoutSubviews {
    NSInteger currCell = self.tag;
    DLX_CellDescriptor *desc = [[DLX_CellDescriptor alloc] initWithRow:currCell];
    CGPoint origin = theLabel.frame.origin;
    CGSize size = theLabel.frame.size;
    CGFloat textHeight = [self getTextHeight:desc.localizedText forLabelWidth:size.width];
    theLabel.frame = CGRectMake(origin.x, origin.y, size.width, textHeight);
    [super layoutSubviews];
}

但这具有不变地确保使用笔尖高度的效果。即使经过检查,textheight它是正确的(并且,对于某些语言,比笔尖的高度大得多。)

4

1 回答 1

1

由于您的单元格相对简单,因此解决问题的一种简单方法是不要使用重用。只需从包中加载 nib 文件。删除那些registerNib相关代码。并这样做:

NSString *reuseIdentifier = @"DistrictCellReuseIdentifier";
DistrictCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
    cell = [[NSBundle mainBundle] loadNibNamed:@"DistrictCell" owner:self options:nil][0];
}
于 2013-09-23T01:57:41.690 回答