2013-10-03 2 views
0

iOS 개발을 처음 사용합니다. 동적 높이 tableView 사용하고 있습니다. tableViewCell의 높이가 증가하거나이 코드를 사용하고 이에 대한 클릭에 감소 ... tableViewCell 높이의 UILabel 높이 조절

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self.selectedPath isEqual:indexPath]) 
    { 
     return 250; 
    } 
    else 
    { 
     return 44; 
    } 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
onSelectCount++; 

    self.selectedPath = indexPath; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationLeft]; 
    self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; 
    [self.tableView1 deselectRowAtIndexPath:indexPath animated:YES]; 

    //First we check if a cell is already expanded. 
    //If it is we want to minimize make sure it is reloaded to minimize it back 
    if(onSelectCount==1) 
    { 
     NSLog(@"num=%d",onSelectCount); 
     NSLog(@"First Condition"); 
     [tableView beginUpdates]; 
     NSIndexPath *previousPath = [NSIndexPath indexPathForRow:self.selectedRowIndex.integerValue inSection:0]; 
     self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; 
     self.selectedPath=indexPath; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView endUpdates]; 
    } 

    if(self.selectedPath.row!=indexPath.row) 
    { 
     [tableView beginUpdates]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView endUpdates]; 

     selectedPath=indexPath; 
     onSelectCount=0; 
     [self tableView:tableView didSelectRowAtIndexPath:selectedPath]; 
    } 

    if(self.selectedRowIndex.integerValue == indexPath.row && onSelectCount==2) 
    { 
     [tableView beginUpdates]; 
     self.selectedRowIndex = [NSNumber numberWithInteger:-1]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     onSelectCount=0; 
     [tableView endUpdates]; 

     } 
} 

지금 나는 tableViewCell에 대한 몇 가지 정보를 표시하는 일부 레이블을 추가 할하지만 난 셀을 클릭 할 때 크기가 조절 완벽하게하지만 셀의 레이블은 크기가 조정되지 않습니다. 셀 높이를 사용하여 UILabels의 크기를 조정할 수있는 방법을 알려주십시오. 어떤 도움을 주시면 감사하겠습니다 ...

답변

0

이 작업을 수행하려면 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 메서드를 사용하십시오. 또한 셀의 높이를 didSelectRow에서 cellForRowAtIndexPath로 변경하는 전체 로직을 이동하는 것이 좋습니다. textLabel라는이 라벨의 이름입니다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    CGRect sizeRect = cell.textLabel.frame; 
    sizeRect.size.height = cell.contentView.frame.size.height; 
    cell.textLabel.frame = sizeRect; 

    return cell; 
} 

:

은 귀하의 질문에 대답합니다. 기본 아이디어는 레이블의 프레임을 가져온 다음 셀의보기 높이에 높이를 설정하고 새 크기를 레이블에 설정하는 것입니다.