2016-06-01 2 views
-1

일부 행을 UITableView에 표시하고 섹션의 헤더에있는 행 수를 표시하지만 행을 삭제하면보기가 매우 이상하게 보임으로 인해 애니메이션이 삭제됩니다 . 내가 뭔가를 놓친다면 내 코드를 살펴 보라.UITableView에서 행 제거 및 섹션 헤더 업데이트

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = [[[UIView alloc]init] autorelease]; 

    view.backgroundColor = [UIColor colorWithRed:(249.0/255.0) green:(249.0/255.0) blue:(249.0/255.0) alpha:1]; 

    UILabel *label = [[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 15)] autorelease]; 
    [label setTextColor:[UIColor colorWithRed:0.56 green:0.56 blue:0.58 alpha:1.0]]; 
    [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:12]]; 
    label.text = @"SELECTED ACCOUNTS ("; 

    NSString *numberOfAccounts = [NSString stringWithFormat:@"%li)", (unsigned long)[self->_selectedAccounts count]]; 

    if([self->_selectedAccounts count]) 
     label.text = [label.text stringByAppendingString:numberOfAccounts]; 

    [view addSubview:label]; 
    [view sizeToFit]; 
    [label sizeToFit]; 
    return view; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     NSMutableArray *selectedAcc = [[NSMutableArray alloc] initWithArray:self->_selectedAccounts]; 
     [selectedAcc removeObjectAtIndex:indexPath.row]; 
     self->_selectedAccounts = selectedAcc; 
     [self->_MyTableView beginUpdates]; 
     [self->_MyTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
     [self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone]; 
     [self->_MyTableView endUpdates]; 
    } 
} 

답변

0

귀하의 경우 조건이 시도 :

[tableView beginUpdates]; 

[yourArray removeObjectAtIndex:myIndexPath.section]; 
[yourArrayAnswerCount removeObjectAtIndex:myIndexPath.section]; 
[tableView deleteSections:[NSIndexSet indexSetWithIndex:myIndexPath.section] withRowAnimation:UITableViewRowAnimationRight]; 


    [tableView endUpdates]; 

이것은 또한 당신을 도울 것입니다 ... https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

+0

감사합니다 도움이 될 것입니다하지만 난 내 코드를 가진 유일한 문제는 그것이 정상적으로 애니메이션되지 않는 것입니다 헤더 행을 삭제하고 업데이트 할 수 있어요 수 내. 행을 삭제하고 헤더를 업데이트하는 두 줄은 다음과 같습니다. '[self -> _ MyTableView deleteRowsAtIndexPaths : @ [indexPath] withRowAnimation : UITableViewRowAnimationTop]; // [self -> _ MyTableView reloadSections : [NSIndexSet indexSetWithIndex : indexPath.section] withRowAnimation : UITableViewRowAnimationNone]; 그래서이 경우에는 섹션 헤더를 업데이트하는 코드에 주석을 달았으므로 이제는 애니메이션이 좋습니다. –

+0

확인. 내 대답에 그 링크를 읽으십시오 .. 그 또한 당신의 문제에 도움이 ... –

+0

예 나는 당신이 준 링크에서 이해하려고합니다. –

0

[self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; 

[self->_MyTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; 
[self->_MyTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone]; 

교체

reloadSections : rowUpdates를 의미해야합니다. 이 방법을 시도 삭제하고 애니메이션 행을 추가하려면이

+0

답장을 보내 주셔서 감사합니다. 그래도 내가 시도했지만 아직 이상한 애니메이션이 있습니다. 나는 원하지 않습니다. @Suraj Sukale 대답에 대한 내 의견을 확인하십시오. –

0

충분해야한다 : -

[tableView beginUpdates]; 
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

deleteIndexPaths은 테이블에서 삭제할 NSIndexPaths의 배열입니다.

NSArray *deleteIndexPaths = [[NSArray alloc] initWithObjects: 
     [NSIndexPath indexPathForRow:0 inSection:0], 
     [NSIndexPath indexPathForRow:1 inSection:0], 
     [NSIndexPath indexPathForRow:2 inSection:0], 
     nil]; 

은 당신에게 답장을

+0

글쎄, 행을 삭제 내 코드에서 잘 작동, 유일한 관심사는 행의 개수가 섹션의 제목 헤더를 업데이 트하는 것입니다. –

관련 문제