Iphone 如何创建可变大小的UITableViewCell?

Iphone 如何创建可变大小的UITableViewCell?,iphone,objective-c,cocoa-touch,uitableview,cs193p,Iphone,Objective C,Cocoa Touch,Uitableview,Cs193p,我似乎无法让文本实际跨越多行。高度看起来正确。我错过了什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] aut

我似乎无法让文本实际跨越多行。高度看起来正确。我错过了什么

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell =  [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"StatusCell"] autorelease];

    CGRect frame = cell.contentView.bounds;
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
    myLabel.text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
    [cell.contentView addSubview:myLabel];

    [myLabel release];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *text = [[person.updates objectAtIndex:indexPath.row] valueForKey:@"text"];
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    CGSize withinSize = CGSizeMake(tableView.frame.size.width, 1000);
    CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];

    return size.height + 20;    
}
另外,我还缺少什么使标签显示得比表格单元格长?


有关如何使用此属性的完整信息,请查看。

Tweetero
中提供了一个示例。其中的代码呈现以下屏幕:

(图取自)

基本实施纲要:

  • 构造
    UITableViewCell
    时,按照
    tableviewCellWithReuseIdentifier:
    中所示的方式创建并添加一个
    UILabel
    作为子视图。查找
    TEXT\u标签的创建

  • 使用视图丰富
    UITableViewCell
    时,请确保正确格式化标签,就像在
    configureCell:forindexath
    中所做的那样,同样地查找标签标签
    TEXT\u标签

  • 返回每个单元格的适当高度,如
    tableView:heightforrowatinexpath
    中所述


  • 问题是它并不总是一个特定的数字,它会为每一行更改numberOfLines属性设置最大行数。将其设置为高值。(正如Rob所说,检查文档。)您还必须将myLabel.lineBrakeMode设置为UILineBreakModeWordWrap。将其设置为0表示“没有最大值”。@Nikolai关于lineBreakMode的看法是正确的;在我的脑海中,WordWrap是UILabel的默认设置,但事实并非如此。
    myLabel.numberOfLines = 2;
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return theSizeYouWantYourCellToBe;
    }