我查看了较旧的问题并尝试了所有建议,但似乎仍然无法让多线UILabel工作。我有一个UITableView,单元格由tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *fieldValue = [self fieldValueAtIndexPath:indexPath];
NSString *fieldName = [self fieldNameAtIndexPath:indexPath];
NSString *title = [[self appDelegate] displayNameForFieldName:fieldName];
Field fieldCode = [[self appDelegate] fieldCodeForFieldName:fieldName];
DetailCell *cell = nil;
NSString *identifier = nil;
BOOL isNotes = [fieldName caseInsensitiveCompare:@"Notes"] == NSOrderedSame;
switch( isNotes ) {
case NO:
{
identifier = @"DetailCell";
cell = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
NSInteger rows = [self heightForText:fieldValue andFont:[self textFont] andWidth:cell.value.frame.size.width] / _oneRowSize.height;
cell.value.text = fieldValue;
cell.name.text = [title lowercaseString];
cell.name.numberOfLines = MAX( 1, rows );
cell.value.numberOfLines = cell.name.numberOfLines;
break;
}
case YES:
{
cell = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCellNotes" forIndexPath:indexPath];
// cell = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DetailCellNotes"];
cell.value.text = @"This is a very long line of text which should take up several lines";
cell.name.text = [title lowercaseString];
cell.value.numberOfLines = 5; // No more than 5 lines of text
cell.value.backgroundColor = [UIColor purpleColor];
cell.value.lineBreakMode = NSLineBreakByWordWrapping;
cell.value.frame = CGRectMake(cell.value.frame.origin.x, cell.value.frame.origin.y, 180, 70);
[cell.value sizeThatFits:CGSizeMake(180., 70.)];
break;
}
}
cell.fieldName = fieldName;
return cell;
}
表格视图中的高度定义如下
- (CGFloat) tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString *fieldName = [self fieldNameAtIndexPath:indexPath];
CGFloat height = 0.0;
if([fieldName isEqualToString:@"Notes"])
{
height = 70.;
}
else if([fieldName isEqualToString:@"Image"])
{
height = 100.;
};
return height;
}
使细胞足够大以容纳3行标签。但是当单元格出现时,标签只有一行(背景显示为紫色)。
tableview使用原型单元格,我也尝试将其设置为numberOfLines = 5和WordWrapping,但这并没有改变效果。我也尝试了两个注释掉的行(尽管搜索表明sizeToFit实际上可能会将numberOfLines重置为1)。
我想知道我错过了什么。我无法看到任何可能被覆盖的地方。
感谢。
答案 0 :(得分:1)
您正在呼叫dequeueReusableCellWithIdentifier:
来创建您的手机。这是一个错误,因为这意味着单元格没有采用其最终尺寸。最好拨打dequeueReusableCellWithIdentifier:forIndexPath:
。这意味着单元格实际上具有您在heightForRowAtIndexPath:
中提供的高度。然后,您应该能够成功设置标签的高度。