如何创建一个可变大小的UITableViewCell? [英] How can I create a variable sized UITableViewCell?

查看:94
本文介绍了如何创建一个可变大小的UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获取文本实际上跨越多行。高度看起来正确。

   - (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 在)。



基本实施纲要:


  1. 当构建 UITableViewCell 时,创建并添加 UILabel code> tableviewCellWithReuseIdentifier:。在丰富 UITableViewCell 时,查找 TEXT_TAG 标签的创建

    / code>与视图,请确保您正确地格式化标签,如 configureCell:forIndexPath 中所做,类似地查找标签标签 TEXT_TAG


  2. 返回每个单元格的适当高度,如 tableView:heightForRowAtIndexPath



I can't seem to get the text to actually span multiple lines. The heights look correct. What am I missing?

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

Also, what am I missing that makes the labels appear longer than the table cell?

解决方案

Tweetero provides an example of this in MessageListController.m. The code there renders the following screen:

(Pic is taken from Mashable).

The basic implementation outline:

  1. When constructing a UITableViewCell, create and add a UILabel as a subview in the manner shown in tableviewCellWithReuseIdentifier:. Look for the creation of TEXT_TAG label.

  2. when enriching the UITableViewCell with views, ensure that you format label properly, as is done in configureCell:forIndexPath, similarly look for the label tag TEXT_TAG.

  3. Return the appropriate height for each cell, as is done in tableView:heightForRowAtIndexPath.

这篇关于如何创建一个可变大小的UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆