2014-04-29 3 views
0

가끔 올바른 UITableViewCell 높이가 표시되지 않습니다. 내 테이블 셀에는 첨부 된 스크린 샷에서 언급 한 세 가지 레이블이 있습니다. 아래의 코드를 확인하고이 문제를 해결할 수 있도록 도와주세요. 사전에올바른 UITableViewCell 높이를 변경할 수 없습니다. 라벨의 텍스트에 따라 ios

-(CGFloat)getTextHeightForIndex:(NSInteger)index 
{ 
    CGSize maximumSize = CGSizeMake(242, 10000); 

    Agenda *agenda = [[Agenda alloc] init]; 
    agenda = [arrayMyAgenda objectAtIndex:index]; 

    NSString *strTotalText = [NSString stringWithFormat:@"%@ %@ %@", agenda.program_name, agenda.program_hall, [NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time]]; 

    CGSize labelHeightSize = [strTotalText sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f] 
            constrainedToSize:maximumSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 
    return labelHeightSize.height +20; 
} 

#pragma mark - UITableViewDelegate 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.arrayMyAgenda count]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [self getTextHeightForIndex:indexPath.row]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Agenda *myAgenda = [[Agenda alloc] init]; 
    myAgenda = [self.arrayMyAgenda objectAtIndex:indexPath.row]; 

    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if(!cell) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

     UILabel *lblTitleText = nil; 
     UILabel *lblAreaText = nil; 
     UILabel *lblTimingText = nil; 

     //First label for Title 
     lblTitleText = [[UILabel alloc] initWithFrame:CGRectMake(10, 6, 305, 24)]; 
     lblTitleText.font = [UIFont fontWithName:@"Helvetica" size:16.0]; 
     lblTitleText.backgroundColor = [UIColor clearColor]; 
     lblTitleText.text = myAgenda.program_name; 
     lblTitleText.textAlignment = NSTextAlignmentLeft; 
     lblTitleText.numberOfLines = 0; 
     lblTitleText.tag = 10; 
     [lblTitleText sizeToFit]; 
     lblTitleText.textColor = [UIColor colorWithRed:(59/255.0) green:(59/255.0) blue:(59/255.0) alpha:1.0]; 
     [cell.contentView addSubview:lblTitleText]; 

     //Second label for Area name 
     lblAreaText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + 4, 305, 21)]; 
     lblAreaText.text = myAgenda.program_hall; 
     lblAreaText.font = [UIFont fontWithName:@"Helvetica" size:16.0]; 
     lblAreaText.backgroundColor = [UIColor clearColor]; 
     lblAreaText.tag = 20; 
     lblAreaText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0]; 
     lblAreaText.textAlignment = NSTextAlignmentLeft; 
     lblAreaText.numberOfLines = 0; 
     [lblAreaText sizeToFit]; 

     [cell.contentView addSubview:lblAreaText]; 

     //Third label for timings 
     lblTimingText = [[UILabel alloc] initWithFrame:CGRectMake(10, lblTitleText.frame.size.height + lblAreaText.frame.size.height + 2, 305, 21)]; 
     lblTimingText.text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time]; 
     lblTimingText.font = [UIFont fontWithName:@"Helvetica" size:16.0]; 
     lblTimingText.backgroundColor = [UIColor clearColor]; 
     lblTimingText.tag = 30; 
     lblTimingText.textColor = [UIColor colorWithRed:(161/255.0) green:(163/255.0) blue:(160/255.0) alpha:1.0]; 
     [cell.contentView addSubview:lblTimingText]; 

     return cell; 
    } 
    else 
    { 
     ((UILabel *)[cell.contentView viewWithTag:10]).text = myAgenda.program_name; 
     ((UILabel *)[cell.contentView viewWithTag:20]).text = myAgenda.program_hall; 
     ((UILabel *)[cell.contentView viewWithTag:30]).text = [NSString stringWithFormat:@"%@ - %@", myAgenda.pg_start_time, myAgenda.pg_end_time]; 

     UIView *selectionColor = [[UIView alloc] init]; 
     selectionColor.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"middleRowSelected.png"]]; 
     cell.selectedBackgroundView = selectionColor; 

     cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
     cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"recentchatcell.jpg"]]; 

     return cell; 
    } 
} 

감사 :

enter image description here

여기 내 코드입니다.

+0

당신은이 대답을 따라야합니다. http://stackoverflow.com/a/21984749/675170 –

답변

0

나는 세 가지 다른 레이블로 표시되는 결합 된 문자열의 높이를 얻는 것이 문제라고 생각합니다. 각 라벨로 이동하는 각 문자열의 높이를 가져와 셀 높이를 가져와 추가해야합니다. 그것들에 표시 될 문자열에서 각 레이블의 높이를 가져와 셀 높이를 얻기 위해 함께 추가하는 것이 더 합리적이지 않습니까?

-(CGFloat)getTextHeightForIndex:(NSInteger)index 
{ 
CGSize maximumSize = CGSizeMake(305, 10000); 

Agenda *agenda = [[Agenda alloc] init]; 
agenda = [arrayMyAgenda objectAtIndex:index]; 

CGFloat cellHeight = 0; 

CGSize titleLabelHeightSize = [agenda.program_name sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f] 
            constrainedToSize:maximumSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 
cellHeight += titleLabelHeightSize.height > 24 ? titleLabelHeightSize.height : 24; 

CGSize areaLabelHeightSize = [agenda.program_hal sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f] 
            constrainedToSize:maximumSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 
cellHeight += areaLabelHeightSize.height > 21 ? areaLabelHeightSize.height : 21; 

CGSize timinglLabelHeightSize = [[NSString stringWithFormat:@"%@ - %@", agenda.pg_start_time, agenda.pg_end_time] sizeWithFont: [UIFont fontWithName:@"Helvetica" size:16.0f] 
            constrainedToSize:maximumSize 
             lineBreakMode:NSLineBreakByWordWrapping]; 
cellHeight += timinglLabelHeightSize.height > 21 ? timinglLabelHeightSize.height : 21; 
return cellHeight +20; 

}

당신은 더이 기능을 일반화 할 수 있지만 나는 이것이 당신의 필요 충분 생각 :

이보십시오.

+0

@HMHeroThanks, 위의 코드를 시도했지만 여전히 내 문제는 동일합니다. – user2786

+0

@ user2786 원래 모양과 똑같은 모양입니까? 실수가있었습니다. CGSize 변경 maximumSize = CGSizeMake (242, 10000); ~ CGSize maximumSize = CGSizeMake (305, 10000); – HMHero

+0

@ user2786 및 제거 [lblTitleText sizeToFit]; 및 [lblAreaText sizeToFit]; – HMHero

1

-(CGFloat)getTextHeightForIndex:(NSInteger)index에서 CGSize 대신 CGRect 대신 사용해보세요. 나를 위해 일했다.

CGRect labelHeightSize = [strTotalText boundingRectWithSize:CGSizeMake(280.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:16.0f]} context:nil]; 
return ceilf(labelHeightSize.size.height) + 50.0f; 
관련 문제