未在分组表视图iphone中显示动态表格单元格高度

时间:2012-03-22 10:18:58

标签: iphone dynamic uitableview height

我有Grouped Table View,我想在其中显示联系人详细信息。但细节是动态长度所以我需要动态高度的细胞。它适用于某些部分,但不适用于remianing部分。这是我的代码

    #define PHONE_DETAIL_SECTION 0
    #define EMAIL_DETAIL_SECION 1
        #define URL_DETAIL_SECTION 2
        #define ADDRESS_DETAIL_SECTION 3
        #define BIRTHDAY_DETAIL_SECTION 4
        #define NOTE_DETAIL_SECTION 5
        #define CONSTRAINT_WIDTH 320.0f
        #define FONT_SIZE 17.0f
        #define MARGIN_SIZE 20.0f
        #define NO_OF_SECTION 6


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return NO_OF_SECTION;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {

        case PHONE_DETAIL_SECTION:
            return [APP_DELGATE.phonenumberDetailLabelArray count];

        case EMAIL_DETAIL_SECION:
            return [APP_DELGATE.emailLabelDetailArray count];

        case URL_DETAIL_SECTION:
            return [APP_DELGATE.urlLabelDetailArray count];

        case ADDRESS_DETAIL_SECTION:
                return [APP_DELGATE.addressDetailLabelArray count]; 

        case BIRTHDAY_DETAIL_SECTION:
            if(birthdateString)
                return 1;
            else
                return 0;

        case NOTE_DETAIL_SECTION:
                if(noteString)
                    return 1;
                else
                    return 0;

        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"section:::%d",indexPath.section);
    static NSString *CellIdentifier = @"Cell";

    // DetailCell is subclass of UITableCell which 
    // contains only 2 labels:- lblName & lblValue

    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
        cell=[cellArray objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    }

    if (indexPath.section==PHONE_DETAIL_SECTION) {

        cell.lblName.text=(NSString*)[APP_DELGATE.phonenumberDetailLabelArray objectAtIndex:indexPath.row];

        cell.lblValue.numberOfLines = 0;
        CGSize suggestedSize = [[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
        NSLog(@" 222222 suggested size height ph no => %f",suggestedSize.height);
        cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);

        cell.lblValue.text=(NSString*)[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row];
    }

    else if(indexPath.section==EMAIL_DETAIL_SECION) {

        cell.lblName.text=(NSString*)[APP_DELGATE.emailLabelDetailArray objectAtIndex:indexPath.row];

        cell.lblValue.numberOfLines = 0;

        CGSize suggestedSize = [[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
        cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);

        cell.lblValue.text=(NSString*)[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row];

    }

    else if(indexPath.section==URL_DETAIL_SECTION) {

        cell.lblName.text=(NSString*)[APP_DELGATE.urlLabelDetailArray objectAtIndex:indexPath.row];

        cell.lblValue.numberOfLines = 0;
        CGSize suggestedSize = [[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
        NSLog(@" 222222 suggested size height url => %f",suggestedSize.height);
        cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);

        cell.lblValue.text=(NSString*)[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row];

    }

    else if (indexPath.section==ADDRESS_DETAIL_SECTION) {

        cell.lblName.text=(NSString*)[APP_DELGATE.addressDetailLabelArray objectAtIndex:indexPath.row];

        cell.lblValue.numberOfLines = 0;
        CGSize suggestedSize = [[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

         cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);

         cell.lblValue.text = (NSString*)[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row];        
    }

    else if(indexPath.section==BIRTHDAY_DETAIL_SECTION) {

        NSString *birthdateStringLocal=[ContactDetails getBirthDate:record];
        cell.lblName.text=BIRTHDAY_LABEL;

        cell.lblValue.text=birthdateStringLocal;
    }

    else if(indexPath.section==NOTE_DETAIL_SECTION) {

        cell.lblName.text=NOTES_LABEL;
        cell.lblValue.numberOfLines = 0;
        CGSize suggestedSize = [noteString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
        cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);

        cell.lblValue.text=noteString;

    }

    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==PHONE_DETAIL_SECTION){

        CGSize suggestedSize = [[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"1111111suggested size height Phone no=> %f",suggestedSize.height);
        return suggestedSize.height + MARGIN_SIZE;
    }

    else if (indexPath.section==ADDRESS_DETAIL_SECTION){

        CGSize suggestedSize = [[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"1111111suggested size height Address=> %f",suggestedSize.height);
        return suggestedSize.height + MARGIN_SIZE;
    }

    else if (indexPath.section==EMAIL_DETAIL_SECION){

        NSLog(@"String 1111: %@", [APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row]);

        CGSize suggestedSize = [[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"1111111suggested size height Email=> %f",suggestedSize.height);
        return suggestedSize.height + MARGIN_SIZE;
    }

    else if (indexPath.section == NOTE_DETAIL_SECTION){
        CGSize suggestedSize = [noteString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"1111111suggested size height NOTES=> %f",suggestedSize.height);
        return suggestedSize.height + MARGIN_SIZE;
    }

    else if (indexPath.section == URL_DETAIL_SECTION){
        CGSize suggestedSize = [[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"1111111suggested size height URL=> %f",suggestedSize.height);
        return suggestedSize.height + MARGIN_SIZE;
    }
    else 
        return 50;
}

我是Address&的动态表格单元格高度注释但不适用于电子邮件,电话号码,URL。如果手机没有或网址足够长,那么它就不显示任何内容。 请参阅以下屏幕截图

enter image description here

enter image description here

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

我认为这是因为你给CONSTRAINT_WIDTH = 320.0f,我认为它大约是200.0F所以做这个改变然后看到结果我认为你的问题已经解决了。 更多的想法是将它用于您的价值标签cell.lblValue.lineBreakMode=UILineBreakModeWordWrap;