UITableViewCell 具有动态内容、动态布局以及动态高度

发布于 2024-09-28 02:53:40 字数 10351 浏览 7 评论 0原文

我有一个 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);
    }


}

非常感谢您的任何建议!

I have a UITableView which sometimes just stops responding when its been loaded. I have no clue why this happens and I was wondering whether it could be the layouting of the cells which I do in cellForRowAtIndexPath, because my content is dynamic and therefore the layout of the subviews has to be adopted each time.

Is there a better approach to achieve what I want? Or is there anything else wrong with this?

Here is the code I wrote:

First the method which creates the custom cell and adds the different views to the content view:

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

}

And here the method which resizes the subviews for each 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;

}

And finally the 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);
    }


}

Thanks a lot for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅暮の光 2024-10-05 02:53:40

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

我还建议缓存可能会很慢的大小测量值。

I think your code is overly complicated and thus inefficient. Maybe you should consider using a custom table view cell which lets you access all the elements clean and directly.

I also suggest to cache the size measurements which are potentially slow.

又怨 2024-10-05 02:53:40

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

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

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

Because you use tag to identify the subview in the cell. Be care of one thing, DON'T use 0 for tag number, or some tag number as the same as the cell's tag value.

The method viewWithTag: will return the first subView with the tag number, if the cell's tag is the same as the one you desired, it will return cell, not a subview.

Eiko's suggestion is right, you should consider using a custom table view cell.

如日中天 2024-10-05 02:53:40

结果发现这是iOS的一个bug。

Turned out that it was an iOS bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文