我已经检查了较老的问题,并尝试了所有的建议,但似乎仍然无法让多行UILabel工作。我有一个由tableView:cellForRowAtIndexPath:创建的UITableView单元格
- (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)。
我想知道我错过了什么。我看不到任何其他地方它可能会被覆盖。
谢谢。
发布于 2014-03-20 10:48:23
您正在调用dequeueReusableCellWithIdentifier:来创建您的单元格。这是一个错误,因为它意味着单元格还没有假定其最终大小。调用dequeueReusableCellWithIdentifier:forIndexPath:要好得多。这意味着单元格实际上将具有您在heightForRowAtIndexPath:中为其指定的高度。然后,您应该能够成功设置标签的高度。
https://stackoverflow.com/questions/22522129
复制相似问题