我有一个动态表格视图,可以从 Web 上的 XML 文件加载数据。原型单元只是两个标签(堆叠)和三个排列在视图底部的按钮。我添加了代码以根据标签的文本长度扩展第二个标签的大小和单元格高度。一些单元格显示完整的第二个标签,而大多数只显示第一行。有时,在我重新滚动到它们(屏幕外到屏幕上)后,单元格会显示完整的第二个标签。这是我的 cellForRowAtIndexPath 代码。
static NSString *CellIdentifier = @"localdiningTableCell";
mhgiLocalDiningCell *diningcell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (diningcell == nil) {
diningcell = [[mhgiLocalDiningCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
mhgiDiningXmlObject *object = [self.xmlParser.allItems objectAtIndex:[indexPath row]];
// Configure the cell...
diningcell.nameLabel.text= [object Name];
NSString *typeText = [object Type];
NSString *addressText = [NSString stringWithFormat:@"%@, %@, %@", [object Street], [object City], [object State]];
NSString *distanceText = [NSString stringWithFormat:@"%@ miles from the hotel", [object Distance]];
NSString *descrText = [NSString stringWithFormat:@"%@\n%@\n%@", typeText, addressText, distanceText];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [descrText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = CGRectMake (diningcell.typeLabel.frame.origin.x, diningcell.typeLabel.frame.origin.y, 260.0f, labelSize.height);
diningcell.typeLabel.frame = frame;
diningcell.typeLabel.lineBreakMode = NSLineBreakByWordWrapping;
diningcell.typeLabel.numberOfLines = 0;
diningcell.typeLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
diningcell.typeLabel.text = descrText;
return diningcell;
例子,
应该看起来像这样
Label 1
Label 2, with some long
text that takes up more than
one line
但看起来像这样
Label 1
Label 2, with some long
.
.
笔记, ”。” 没有实际显示,只是表示提供了标签的空间。文本似乎在随机单元格上消失了。