2014-02-09 3 views
4

UIView 애니메이션을 사용하여 UITableViewCell 높이를 변경하는 애니메이션을 만들고 싶습니다 (앞으로 봄 애니메이션 사용).UITableViewCell의 높이를 변경하기위한 사용자 정의 애니메이션

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundColor = [UIColor greenColor]; 
    } 
    UILabel *viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, [[UIScreen mainScreen] bounds].size.width, 40.0f)]; 
    viewLabel.text = @"Test"; 
    viewLabel.backgroundColor = [UIColor clearColor]; 
    [cell.contentView addSubview:viewLabel]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView beginUpdates]; 
    [self animateCell:indexPath andTableView:tableView]; 
    [tableView endUpdates]; 
} 


- (void)animateCell:(NSIndexPath*)indexPath andTableView:(UITableView*)tableView 
{ 
    [UIView animateWithDuration:1.0f animations:^
    { 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     CGRect rect = cell.frame; 
     rect.size.height = 90.0f; 
     cell.frame = rect; 
     NSLog(@"%f", cell.frame.size.height); 
    }]; 
} 

을하지만 그것은 작동하지 않습니다

그래서 내가있다. 하지만 난`는 추가하는 경우 :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath isEqual:[tableView indexPathForSelectedRow]]) 
    { 
     return 90.0f; 
    } 
    return 40.0f; 
} 

내가 셀 animateCell 방법에서 생성되는 사용자 정의 셀에 적용되는 높이와 애니메이션을 변경하는 것을 볼

+0

는, jQuery과 기본 애니메이션을 수행 여기 tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _selectedIndexPath = indexPath; [UIView animateWithDuration:1 animations:^{ // move down cells which are below selected one for (UITableViewCell *cell in tableView.visibleCells) { NSIndexPath *cellIndexPath = [tableView indexPathForCell:cell]; if (cellIndexPath.row > indexPath.row) { CGRect frame = cell.frame; frame.origin.y += kTargetHeight - tableView.rowHeight; cell.frame = frame; } } // expand selected cell UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; CGRect frame = cell.frame; frame.size.height = kTargetHeight; cell.frame = frame; } completion:^(BOOL finished) { [tableView reloadData]; // commit final state }]; } 

선택한 셀의 최종 상태에서 애니메이션의 예입니다. 애니메이션 속성은 테이블 뷰의 데이터 소스 값을 기반으로합니다. – vokilam

+0

그래서 beginUpdates와 endUpdates를 호출 할 필요가 없으며 맞춤 애니메이션 (셀 근처의 위치를 ​​애니메이션으로 변경)을 처리 할 필요가 없습니까? – Dimitrio

+1

애니메이션은 무엇이되어야합니까? – vokilam

답변

5

난 당신이 beginUpdate/endUpdates 방법을 사용해서는 안 생각 reloadData을 호출하기 전에 수동으로 애니메이션을 수행하십시오. 애니메이션 이후의 셀의 최종 상태는 데이터 소스로 정의됩니다. 당신이 EndUpdates 사이를 호출 할 때

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([indexPath isEqual:_selectedIndexPath]) { 
     return kTargetHeight; 
    } 

    return tableView.rowHeight; 
} 
+0

감사합니다. 작동하지만 깜박임 [tableView reloadData] – Dimitrio

+1

감사합니다. 작동하지만 깜박임으로 인한 것입니다 [tableView reloadData] This 더 나은 모습을 제공합니다 : [tableView beginUpdates]; [tableView endUpdates]; – Dimitrio

+0

아시다시피, didDeselectRowAtIndexPath와 비슷합니다. 작동하지 않습니다. – Dimitrio

관련 문제