我想创建一个tableview,其中的单元格的高度是动态的。UITableViewCell里面的UILabel高度
到目前为止,我设法根据我添加的自定义UILabel设置单元格的高度。
与常规cell.textLabel它工作正常,但是当我使用我自己的标签出问题了。我只看到了一半的标签,但是当我上下滚动时,有时标签会延伸并显示所有文本......您可以看到标签应该在图像中结束的位置。
这是cellForRowAtIndexPath
中的文本:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
Car *carForCell = [cars objectAtIndex:indexPath.row];
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.numberOfLines = 0;
nameLabel.text = carForCell.directions;
[nameLabel sizeToFit];
[nameLabel setBackgroundColor:[UIColor greenColor]];
return cell;
我已经在'tableView:heightForRowAtIndexPath'方法中设置了单元格的高度,如图所示。我改变了'cellForRowAtIndexPath'中的代码,结果如下:[link](http://cl.ly/image/063e3Y0N2E05)。所有的文字都很引人注目,但宽度看起来很乱。 – 2013-02-08 18:36:23
我认为这是'sizeToFit:'的问题 - 你可以使用'sizeWithFont:sizeWithFont:forWidth:lineBreakMode:'来计算标签大小吗? – ChrisH 2013-02-08 18:43:38
似乎工作。在做出正确答案之前,我会再试一试。 :-) – 2013-02-08 18:52:06