从Nib加载的UITableViewCell中的UILabel大小调整

时间:2013-09-22 23:50:08

标签: ios objective-c uitableview

Cell中的UILabel包含NSLocalizedString;根据语言,从几个字符到一个段落不等。 UILabel的框架有时会这样做,有时不会绘制以适应字符串长度。我认为这是重用的问题,但我对如何解决它感到茫然。

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

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:,使用它来确定单元格高度并将结果存储到名为labelHeightforRow.的NSMutableArray中。到目前为止,一切都运行良好。

我按原样创建表:

- (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变为代码中指定的正确高度;显示整个字符串。然后,如果重建了单元格;比如,在它滚出屏幕并且其中一个单元格类型已经绘制之后,长文本标签再次使用Nib的截断高度。

我尝试将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也是正确的(对于某些语言,它比Nib的高度大得多。)

1 个答案:

答案 0 :(得分:1)

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

NSString *reuseIdentifier = @"DistrictCellReuseIdentifier";
DistrictCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
    cell = [[NSBundle mainBundle] loadNibNamed:@"DistrictCell" owner:self options:nil][0];
}