UILabel in a Cell 包含一个 NSLocalizedString;根据语言的不同,从几个字符到一个段落不等。UILabel 的框架有时会,有时不会绘制以适应字符串长度。我相信这是重用的问题,但我不知道如何解决它。
我有一系列五个不同的UITableViewCell子类,从Nibs(项目要求(加载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
是正确的(对于某些语言,比笔尖的高度大得多(。
由于您的单元格相对简单,因此解决问题的一种简单方法是不要使用重用。只需从捆绑包加载笔尖文件即可。删除这些registerNib
相关的代码。并像这样做:
NSString *reuseIdentifier = @"DistrictCellReuseIdentifier";
DistrictCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:@"DistrictCell" owner:self options:nil][0];
}