2011-05-06 5 views
1

fetchedResultsController가 지원하는 테이블 뷰가 있습니다. 내 테이블 셀에는 뷰가 걱정되는 한 소프트 삭제를 수행하는 버튼이 있습니다 ... 버튼을 탭하고 소프트 삭제를 수행하는 선택기가 호출되며 ... 무언가가 발생해야합니다. 내가 많은 시도를 해봤지만) 애플의 행 애니메이션과 같은 애니메이션을 시도하지 않았다.Core Data/UITableView에서 데이터 새로 고침 (소프트 삭제)

길을 벗어난 슬라이드 만들기와 같이 간단한 행 애니메이션을 수행 할 수 있습니다. 테이블을 다시로드하라는 메시지가 나올 때까지 빈 행을 남겨 둡니다. 이는 약간의 반항 효과입니다. 이것은 내가 왔어요 가장 가까운 :

 -(void) completeTask: (id) sender { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 

    NSDate *date = [[NSDate alloc] init]; 
    NSManagedObject *task = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    [task setValue:date forKey:@"complete"]; 

    AppController *ac = [AppController sharedAppController]; 
    NSManagedObjectContext *moc = [ac managedObjectContext]; 

    NSError *error = nil; 
    if(![moc save:&error]){ 
     NSLog(@"%@ %@",error, [error description]); 
     exit(-1); 
    } else { 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

     CGRect cellFrame = cell.frame; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.35]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDidStopSelector:@selector(someAnimationDidStop:finished:context:)]; 
     cell.frame = CGRectMake(cellFrame.size.width , cellFrame.origin.y, cellFrame.size.width, cellFrame.size.height); 
     [UIView commitAnimations]; 
    } 
} 

-(void) someAnimationDidStop:(NSString *)animationID 
        finished:(BOOL)finished context:(void *)duration { 
    NSLog(@"Animation Stopped"); 
    [self fetchResults]; // this guy performs a new fetch and table data reload 
} 

내가 바른 길에 일종의 것 같은 내가 그러나 나는이 정말 꽤 대답은 생각하지 않는다 생각합니다. 나는 어떻게 든 controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:가 응답이되기를 바랐다. 내 생각 엔 재고가있는 Apple 애니메이션을 원한다면 결과를 추적하고 동기화 상태를 유지하기 위해 별도의 NSMutableArray를 만들어야합니다.

생각과 의견?

감사합니다 !!!

답변

1

난 당신이 이미 그 "소프트 삭제"개체를 가져 오지 않는 NSFetchedResultsController에 대한 조건을 추측 :

다음은 전체 그림입니다.

따라서 이론적으로는 모두NSFetchResultsControllerDelegate 개의 방법으로 구현해야합니다. 문서의 샘플 구현은 정상적으로 작동합니다.
그냥 개체에서 "소프트 삭제"값을 변경, 수동으로 관리하는 세포 대신 그런 다음 NSFetchedResultsController

delegate을 설정하는 것을 잊지 마십시오.NSFetchResultsController는 컨텍스트를 모니터링 할 것이고,이 변경으로 인해 객체가 컬렉션에서 사라져서 tableView에서 셀이 제거된다는 것을 알 수 있습니다.

+0

맞습니다 - 실제 삭제를 올바르게 사용하기 위해 대리인 메서드를 일부 변경 한 후 수동 테이블 관리를 제거 할 때까지 아래의 내 대답이 작동하지 않는 것으로 나타납니다. 감사! – Cameron

1

EDIT : @fluchtpunkt 대답이 맞습니다. 내 코드를 약간 더 업데이트 한 후 내 솔루션이 다시 고장났습니다. FRC를 다시 페치하는 블록을 제거하면 다시 작동합니다.


어제 나는 모든 반복적 인 테스트 (학습)에서 이것을 이해하지 못함에도 놀랐다. 필자는 핵심 데이터 가져 오기 결과를 테이블보기와 좋은 방법으로 동기화하는 문제에 직면 해있었습니다. 종종 잘못된 색인을 얻습니다. (행 수 (#)는 숫자 #와 같거나 행 수를 뺀 숫자 (0 삽입 : # 삭제) .. 등).

  1. 새가
  2. 다음 행을 삭제 가져 마십시오 전무 self.fetchedrResultsController = nil
  3. 에 가져온 결과 컨트롤러를 설정 & 저장 소프트 삭제 수행하여 다음과 같이이 작업을 완료하는 데

    내 단계는 있었다 테이블에서 :

   self.fetchedResultsController = nil; 
     NSError *error = nil; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } else { 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
     } 

나는 왜 이것을 이전에 이해하지 못했는지 정말로 모르지만, 그렇게 많은 의미가 있습니다. 신선한 눈?

 
-(void) completeTask: (id) sender { 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 

    NSManagedObject *task = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    [task setValue:date forKey:@"complete"]; 

    AppController *ac = [AppController sharedAppController]; 
    NSManagedObjectContext *moc = [ac managedObjectContext]; 

    NSError *error = nil; 
    if(![moc save:&error]){ 
     NSLog(@"Uh oh - couldn't save the delete! %@ \n%@",error, [error description]); 
     exit(-1); 
    } else { 

     // Save successful; re-fetch data (background) 
     self.fetchedResultsController = nil; 
     NSError *error = nil; 
     if (![[self fetchedResultsController] performFetch:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } else { 
      //update the table view with row animation 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationRight]; 
     } 
    } 
}