2013-09-22 66 views
0

UILabel在Cell中包含NSLocalizedString;取决于语言,从几个字符到一个段落。有时候UILabel的框架会这样做,并且有时不会绘制以适应字符串长度。我相信这是一个重用问题,但我对如何解决这个问题不知所措。从Nib加载的UITableViewCell中的UILabel大小调整

我在viewDidLoad一系列从笔尖(项目要求)装载五种不同的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是笔尖指定的高度:截断超长文字和下面给出一个大的白色空间(来自正确的细胞高度调整)。当表格被重新绘制时,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是正确的(并且对于某些语言来说,远大于笔尖的高度)。

回答

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]; 
} 
+0

我彻底简化了问题中提供的示例代码,以避免混淆问题。实际的单元具有UIControls,图像,背景,手风琴功能等。但是,我不熟悉关闭重用。你能否详细说明一下? – NSConfusedCoder

+0

@NSConfusedCoder答案已更新。 – sunkehappy

+0

建议的代码(如果以键入方式实现的话)会导致获得相同错误的不同方法。但是,它确实指出了我走向正确的道路。我相信修复重用问题是确保reuseIdentifier是唯一的,像'NSString * reuseIdentifier = [NSString stringWithFormat:@“CellReuseIdentifier%d”,indexPath.row];' – NSConfusedCoder

相关问题