iphone - 如何创建可变大小的 UITableViewCell?

标签 iphone objective-c cocoa-touch uitableview cs193p

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

- (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;    
}

此外,我遗漏了什么使标签看起来比表格单元格长? alt text

最佳答案

TweeteroMessageListController.m 中提供了一个例子.那里的代码呈现以下屏幕:

(图片来自Mashable)

基本实现大纲:

  1. 在构建 UITableViewCell 时,按照 tableviewCellWithReuseIdentifier: 中所示的方式创建并添加一个 UILabel 作为 subview 。寻找 TEXT_TAG 标签的创建。

  2. 当使用 View 丰富 UITableViewCell 时,请确保正确格式化标签,就像在 configureCell:forIndexPath 中所做的那样,类似地查找标签标签 TEXT_TAG

  3. 返回每个单元格的适当高度,如 tableView:heightForRowAtIndexPath 中所做的那样。

关于iphone - 如何创建可变大小的 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1233608/

相关文章:

ios - 在 Swift 中从字符串转换为 int

javascript - webview 与 native iOS 应用程序通信的任何方式?

iphone - 如何快速调用方法但最早在下一次运行循环迭代中?

iphone - 在 UITextview 中禁用放大镜

iphone - 识别当前到达的响应的请求对象restkit v0.20

iphone - NSDateFormatter - 设置日期格式

objective-c - 如何制作一个小的透明模态整体来表示加载数据?

ios - 遇到多个 NSURLConnection 问题

ios - 在 iOS 中使用文本字段和按钮调用电话

iphone - 从 iPhone 麦克风检测节拍