使用自动UITableViewCellHeight的奇怪结果

时间:2011-08-30 10:00:18

标签: ios ios4 uitableview custom-cell

我几乎成功地集成了自动UITableViewCellHeight的东西,但是很奇怪。如果我运行它,它会显示第三部分第一部分中的文本(尽管也是第一部分)。有想法该怎么解决这个吗?

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    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);
            }
}

1 个答案:

答案 0 :(得分:1)

尝试为每个部分使用不同的CellIdentifier

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