努力在UITableView中格式化动态行高 [英] struggling with formatting dynamic row height in UITableView

查看:139
本文介绍了努力在UITableView中格式化动态行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据文本长度调整UITableView中行的高度。我有以下代码:

I am trying to resize the height of my row in UITableView based on the text length. I have the following code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText =[[topics objectAtIndex:indexPath.row] name];
    UIFont *cellFont = [UIFont fontWithName:@"ArialMT" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:17.0];
    }
}

然而,它与UIImageView和UIDetailText混淆,图片如下所示:

However, it messes up with the UIImageView and the UIDetailText, image shown below:

如何解决这个问题?

我试过了:

[cell.imageView setContentMode:UIViewContentModeScaleToFill];
    [cell.imageView setFrame:CGRectMake(0, 0, 16,16)];
    [cell.imageView setBounds:CGRectMake(0, 0, 16,16)];
    [cell.imageView setAutoresizingMask:UIViewAutoresizingNone];
    [cell.imageView setAutoresizesSubviews:NO];

且似乎没有工作

推荐答案

您可以将自己的子视图添加到单元格的内容视图中,而不是按照其他人的建议进行子类化。

Instead of subclassing as suggested by others, you could also add your own subviews to the cell’s content view.

来自自定义单元格


如果您希望单元格具有不同的
内容组件并拥有这些
布局在不同的位置,或者如果
你想要单元格的不同行为
特征,你有
两个替代品。您可以将子视图
添加到
单元格对象的contentView属性中,也可以创建UITableViewCell的自定义
子类。

If you want the cell to have different content components and to have these laid out in different locations, or if you want different behavioral characteristics for the cell, you have two alternatives. You can add subviews to the contentView property of the cell object or you can create a custom subclass of UITableViewCell.


  • 如果可以使用适当的自动调整设置完全指定内容布局,并且不需要修改单元格的默认行为,则应将子视图添加到单元格的内容视图中。

  • 当您的内容需要自定义布局代码或需要更改单元格的默认行为时,应该创建自定义子类,例如响应编辑模式。

参见此示例:

#define CUSTOM_IMAGE_TAG 99
#define MAIN_LABEL 98

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UIImageView *customImageView = nil;
    UILabel *mainLabel = nil;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        customImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)] autorelease];
        customImageView.tag = CUSTOM_IMAGE_TAG;
        [cell.contentView addSubview:customImageView];

        mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(60.0f, 10.0f, 100.0f, 21.0f)] autorelease];
        mainLabel.tag = MAIN_LABEL;
        mainLabel.numberOfLines = 0;
        [cell.contentView addSubview:mainLabel];
    } else {
        customImageView = (UIImageView *)[cell.contentView viewWithTag:CUSTOM_IMAGE_TAG];
        mainLabel = (UILabel *)[cell.contentView viewWithTag:MAIN_LABEL];
    }

    // Configure the cell.
    CGRect frame = mainLabel.frame;
    frame.size.height = ... // dynamic height
    mainLabel.frame = frame;

    return cell;
}

显然,你仍然需要实现 tableView:heightForRowAtIndexPath :

Obviously, you still need to implement tableView:heightForRowAtIndexPath:.

这篇关于努力在UITableView中格式化动态行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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