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
是正確的(並且對於某些語言來說,遠大於筆尖的高度)。
我徹底簡化了問題中提供的示例代碼,以避免混淆問題。實際的單元具有UIControls,圖像,背景,手風琴功能等。但是,我不熟悉關閉重用。你能否詳細說明一下? – NSConfusedCoder
@NSConfusedCoder答案已更新。 – sunkehappy
建議的代碼(如果以鍵入方式實現的話)會導致獲得相同錯誤的不同方法。但是,它確實指出了我走向正確的道路。我相信修復重用問題是確保reuseIdentifier是唯一的,像'NSString * reuseIdentifier = [NSString stringWithFormat:@「CellReuseIdentifier%d」,indexPath.row];' – NSConfusedCoder