开发者

Weird result with automatic UITableViewCellHeight

开发者 https://www.devze.com 2023-04-01 18:18 出处:网络
i nearly successfully integrated the automatic UITableViewCellHeight thingy, butone thing is very strange. If i run it, it shows me the text from the first section in the third section (and also in th

i nearly successfully integrated the automatic UITableViewCellHeight thingy, butone thing is very strange. If i run it, it shows me the text from the first section in the third section (and also in the first one though). Any ideas on how to fix this?

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)i开发者_Python百科ndexPath
{
    static NSString *CellIdentifier = @"Cell";
    UILabel *label;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        label = [[[UILabel alloc] initWithFrame:CGRectZero]autorelease];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [label setMinimumFontSize:FONT_SIZE];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label setTag:1];
        [[cell contentView] addSubview:label];

        if (indexPath.section == 0) {        
        NSString *text = @"abc.";
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        if (!label)
            label = (UILabel*)[cell viewWithTag:1];

        [label setText:text];
            [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
        }

        else
            if (indexPath.section == 1) {
                NSString *text = @"def.";
                CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
                CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

                if (!label)
                    label = (UILabel*)[cell viewWithTag:1];

                [label setText:text];
                [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
            }

            else
                if (indexPath.section == 2) {
                    NSString *text = @"ghi.";
                    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
                    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

                    if (!label)
                        label = (UILabel*)[cell viewWithTag:1];

                    [label setText:text];
                    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
                }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (indexPath.section == 0) {
        NSString *text = @"abc.";
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        CGFloat height = MAX(size.height, 44.0f);

        return height + (CELL_CONTENT_MARGIN * 2);
    }
    else
        if (indexPath.section == 1) {
            NSString *text = @"def.";
            CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

            CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

            CGFloat height = MAX(size.height, 44.0f);

            return height + (CELL_CONTENT_MARGIN * 2);
        }

        else
            if (indexPath.section == 2) {
                NSString *text = @"ghi.";
                CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

                CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

                CGFloat height = MAX(size.height, 44.0f);

                return height + (CELL_CONTENT_MARGIN * 2);
            }
}


try to use different CellIdentifiers for each section

static NSString *CellIdentifier;
switch(indexPath.section){
    case 0:
        CellIdentifier = @"first";
        break;
    case 1:
        CellIdentifier = @"second";
        break;
    case 2:
        CellIdentifier = @"third";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号