1

好吧,我一直在寻找很多东西来解决我的问题,但是,我没有发现任何有用的东西。

我有我的自定义单元格,它可以(或不能)以自由顺序包含图像、文本、标题等,因此我需要根据其内容设置行的高度。喜欢这两个: http: //db.tt/AVBKYuEY http://db.tt/HbnXMMFn

我可以制作它们的唯一方法是通过故事板。

所以,我调用tableView:cellForRowAtIndex: 并在那里设置全部内容,然后我想设置行的高度,但我不能这样做,因为行的高度设置在tableView:height...: 中,这被称为 BEFOREtableView:cellForRowAtIndex:

顺便说一句,我的情节提要中也有限制,所以,如果我可以使用它们来计算单元格高度,那就太好了。

而且当我旋转iPhone时它也完美地改变了宽度,事实上我不能改变高度很奇怪

如何解决我的问题?

4

1 回答 1

1

您可以在 tableView:heightForRowAtIndexPath: 中设置高度。您可以在该方法中访问索引路径,因此可以访问向单元格提供数据的数组的索引。因此,您需要查看该数据,并进行确定单元格高度所需的任何计算,并返回该值。

编辑后:这是一个计算多行标签高度的示例。我创建了一个临时标签,用该行将包含的文本填充它,然后使用 sizeWithFont:constrainedToSize:LineBreakMode: 进行计算:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0; // allows label to have as many lines as needed
    label.text = _objects[indexPath.row][@"detail2"]; // this is the data I'm passing in the detail label
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(300, 300000) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat h = labelSize.height;
    return h + 50; //50 is base height for my cell with only one line of text, determined empirically
}
于 2012-12-11T16:27:37.620 回答