具有动态内容、动态布局和动态高度的 UITableViewCell

Posted

技术标签:

【中文标题】具有动态内容、动态布局和动态高度的 UITableViewCell【英文标题】:UITableViewCell with dynamic content, dynamic layout and thus a dynamic height 【发布时间】:2010-10-20 08:59:49 【问题描述】:

我有一个 UITableView,它有时会在加载时停止响应。我不知道为什么会发生这种情况,我想知道这是否可能是我在 cellForRowAtIndexPath 中所做的单元格布局,因为我的内容是动态的,因此每次都必须采用子视图的布局。

有没有更好的方法来实现我想要的?或者这还有什么问题吗?

这是我写的代码:

首先创建自定义单元格并将不同视图添加到内容视图的方法:

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier 


    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];


    UILabel *titleLbl, *amountLbl, *title2Lbl, *numberLbl, *mergeLbl, *typeLbl;


    UIColor *bgColor, *blackColor, *amountColor , *title2Color;
    UIColor *clearColor = [UIColor clearColor];


    bgColor = [UIColor clearColor];
    title2Color = [UIColor colorWithRed:0.1 green:0.1 blue:0.6 alpha:1];
    amountColor = [UIColor redColor];
    blackColor = [UIColor blackColor];

    //The number
    numberLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    numberLbl.tag = kNumberLblTag;
    numberLbl.font = [UIFont systemFontOfSize:12];
    numberLbl.textAlignment = UITextAlignmentLeft;
    numberLbl.textColor = blackColor;
    numberLbl.backgroundColor = clearColor;
    numberLbl.numberOfLines = 0;
    [cell.contentView addSubview:numberLbl];
    [numberLbl release];

    amountLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    amountLbl.tag = kamountLblTag;
    amountLbl.font = [UIFont systemFontOfSize:12];
    amountLbl.textAlignment = UITextAlignmentLeft;
    amountLbl.textColor = amountColor;
    amountLbl.backgroundColor = clearColor;
    amountLbl.numberOfLines = 0;
    [cell.contentView addSubview:amountLbl];
    [amountLbl release];


    mergeLbl = [[UILabel alloc] initWithFrame:CGRectZero] ;
    mergeLbl.tag = kMergeLblTag;
    mergeLbl.font = [UIFont systemFontOfSize:12];
    mergeLbl.textAlignment = UITextAlignmentLeft;
    mergeLbl.textColor = blackColor;
    mergeLbl.backgroundColor = clearColor;
    mergeLbl.numberOfLines = 0;
    [cell.contentView addSubview:mergeLbl];
    [mergeLbl release];


    typeLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    typeLbl.tag = kTypeLblTag;
    typeLbl.font = [UIFont systemFontOfSize:12];
    typeLbl.textAlignment = UITextAlignmentLeft;
    typeLbl.textColor = blackColor;
    typeLbl.backgroundColor = clearColor;
    typeLbl.numberOfLines = 1;
    [cell.contentView addSubview:typeLbl];
    [typeLbl release];


    titleLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLbl.tag = kTitleLblTag;
    titleLbl.font = [UIFont systemFontOfSize:14];
    titleLbl.textAlignment = UITextAlignmentLeft;
    titleLbl.textColor = blackColor;
    titleLbl.backgroundColor = clearColor;
    titleLbl.numberOfLines = 0;
    [cell.contentView addSubview:titleLbl];
    [titleLbl release];


    title2Lbl = [[UILabel alloc] initWithFrame:CGRectZero];
    title2Lbl.tag = ktitle2LblTag;
    title2Lbl.font = [UIFont systemFontOfSize:12];
    title2Lbl.textAlignment = UITextAlignmentLeft;
    title2Lbl.textColor = title2Color;
    title2Lbl.backgroundColor = clearColor;    
    title2Lbl.numberOfLines = 0;
    [cell.contentView addSubview:title2Lbl];
    [title2Lbl release];


    UIView *bgView = [[UIView alloc]initWithFrame:CGRectZero];
    bgView.backgroundColor = bgColor;
    cell.backgroundView = bgView;
    [bgView release];


    NSLog(@"tableViewCellWithReuseIdentifier");

    return cell;


这里是调整每个单元格子视图大小的方法:

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



    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];



    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];

    NSUInteger r = row+(section*100);

    if (cell == nil) 

        cell = [self tableViewCellWithReuseIdentifier: CellIdentifier];

    

    UIColor *bgColor, *clearColor = [UIColor clearColor];





    //set the rePointer to the actual array 
    if (r < [results count]) 
        rePointer = results;
        //NSLog(@"results r: %i", r);
    else 
        r = [indexPath row];
        rePointer = rejects;
        //NSLog(@"rejects r: %i", r);

    



    NSString *noTxt = [[NSString alloc]initWithFormat:@"%i.", r+1];
    CGSize boundingSize = CGSizeMake(35, 100);
    UIFont *sysFont = [UIFont systemFontOfSize:12]; 
    CGSize requiredSize = [noTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    CGFloat requiredHeight = requiredSize.height;

    UILabel *numberLbl = (UILabel*)[cell viewWithTag:kNumberLblTag];
    numberLbl.frame = CGRectMake(5, 5, 35, requiredHeight);
    numberLbl.text = noTxt;





    NSMutableString *amountTxt = [[NSMutableString alloc]initWithString:@"amount: "];
    [amountTxt appendString:[NSString stringWithFormat:@"%@", [[rePointer objectAtIndex:r] objectForKey:@"amount"]]];


    boundingSize = CGSizeMake(35, 100);
    sysFont = [UIFont systemFontOfSize:12]; 
    requiredSize = [amountTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    UILabel *amountLbl = (UILabel*)[cell viewWithTag:kamountLblTag];
    amountLbl.frame = CGRectMake(5, 20, 35, requiredHeight);
    amountLbl.text = amountTxt;





    if ([[[rePointer objectAtIndex:r] objectForKey:@"count"] intValue] > 0) 

        NSString *mergedTxt = [[NSString alloc]initWithString:@"Count"];
        boundingSize = CGSizeMake(35, 100);
        sysFont = [UIFont systemFontOfSize:12]; 
        requiredSize = [mergedTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
        requiredHeight = requiredSize.height;


        UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
        mergeLbl.frame = CGRectMake(5, 50, 35, requiredHeight);
        mergeLbl.text = mergedTxt;
        [mergedTxt release];

    else 

        UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
        mergeLbl.frame = CGRectZero;
        mergeLbl.text = @"";

    



    NSString *type;

    if (![[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"NotSet"]) 
        if ([[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"[XYZ]"]) 
            type = @"X";
        else
            int typeLength = [[[rePointer objectAtIndex:r] objectForKey:@"type"] length];
            type = [[[rePointer objectAtIndex:r] objectForKey:@"type"] substringWithRange:NSMakeRange(1, typeLength-2)];
        


        UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
        typeLbl.frame = CGRectMake(5, 65, 35, 20);
        typeLbl.text = type;

    else 
        UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
        typeLbl.frame = CGRectZero;
        typeLbl.text = @"";
    






    NSString *titleTxt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"h_title"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];


    boundingSize = CGSizeMake(190, 1000);
    sysFont = [UIFont systemFontOfSize:14];

    requiredSize = [titleTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    CGFloat titleHeight = requiredHeight;

    UILabel *titleLbl = (UILabel*)[cell viewWithTag:kTitleLblTag];
    titleLbl.frame = CGRectMake(50, 5, 190, requiredHeight);
    titleLbl.text = titleTxt;
    [titleTxt release];





    NSString *title2Txt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"title2"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];

    sysFont = [UIFont systemFontOfSize:12];
    requiredSize = [title2Txt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    UILabel *title2Lbl = (UILabel*)[cell viewWithTag:ktitle2LblTag];
    title2Lbl.frame = CGRectMake(50, titleHeight + 10, 190, requiredHeight);
    title2Lbl.text = title2Txt;
    [title2Txt release];




    /*
    CGFloat height = [[[rePointer objectAtIndex:r] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
    height += [[[rePointer objectAtIndex:r] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
    height += 20;
    */

    cell.contentView.backgroundColor = clearColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    NSLog(@"cellForRowAtIndexPath");




    /*
    cell.backgroundView.frame = CGRectMake(0, 0, 320, height-1);
    cell.backgroundView.backgroundColor = bgColor;
    */



    return cell;


最后是 heightForRowAtIndexPath:

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


    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    row += section*100;

    NSUInteger rejectsIndex = [indexPath row];

    if (row < [results count]) 
        CGFloat height = [[[results objectAtIndex:row] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += [[[results objectAtIndex:row] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += 20;
        return MAX(90, height);
    else 
        CGFloat height = [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += 20;

        return MAX (90, height);
    



非常感谢您的建议!

【问题讨论】:

【参考方案1】:

我认为您的代码过于复杂,因此效率低下。也许您应该考虑使用自定义表格视图单元格,它可以让您直接访问所有元素。

我还建议缓存可能很慢的尺寸测量值。

【讨论】:

嗯,我考虑过继承 UITableViewCell。但是我仍然必须每次都对单元格进行布局。那为什么会好很多呢?缓存尺寸测量值究竟是什么意思? 我只是按照您的建议尝试对 UITableViewCell 进行子类化。但是,我的应用程序有时只是在显示 tableview 后没有响应。主要是当我在导航堆栈层次结构中来回走动时。 您计算所需的高度并一遍又一遍地设置元素的位置 - 但如果这真的是问题还不清楚。您可能需要使用仪器进行一些性能日志记录(进行一些滚动并查看大多数周期花费的位置)。 嗯,首先我必须一遍又一遍地计算尺寸。在此示例中可能没有任何意义,因为我将标签放置在固定位置。但在我的“真实代码”中,这是动态的,因此每次都必须重新计算和重置标签的位置和帧。但这似乎不是问题。滚动执行得很好。根据 Instruments 的说法,它也不占用太多内存。问题真的很奇怪。快速返回到 rootViewController 后,UITableViewController 显示但不再响应。【参考方案2】:

因为您使用标签来标识单元格中的子视图。请注意一件事,不要使用 0 作为标签号,或者某些标签号与单元格的标签值相同。

viewWithTag: 方法将返回第一个带有标签号的子视图,如果单元格的标签与您想要的相同,它将返回单元格,而不是子视图。

Eiko 的建议是对的,您应该考虑使用自定义表格视图单元格。

【讨论】:

【参考方案3】:

原来是 ios 的 bug。

【讨论】:

想详细说明一下?我知道已经有一段时间了,但是您应该分享您的发现以帮助其他正在寻找答案的人:-)

以上是关于具有动态内容、动态布局和动态高度的 UITableViewCell的主要内容,如果未能解决你的问题,请参考以下文章

如何根据其中的内容创建具有动态高度的集合视图?

UITableViewCell 具有嵌入式垂直堆栈视图设置,具有自动布局和动态高度

具有动态高度的 UITableView 和 UITableViewCell 仅在 WillDisplay 中知道

当项目具有动态高度时,如何根据设备方向创建动态颤动网格布局

具有自动布局的自定义单元格的动态高度

具有自动布局的 UITableView 中的动态单元格高度