0

테이블 섹션과 sortDescriptors 내에 뭔가가있어 나를 미치게합니다. 어떤 도움을 많이 주시면 감사하겠습니다;)빈 sortDescriptors 및 테이블 뷰 섹션

내가 찾고있는 것은 "add"행을 각 self.fetchedResultsController.fetchedObjects 섹션의 하단에 표시하는 것입니다. sectionNameKeyPath를 사용하여 테이블보기의 섹션을 표시합니다. 여기까지 모든 것이 완벽하게 작동합니다. 내 애플 리케이션의 다른 부분 에서이 기술을 구현하고 꽤 잘 작동합니다.

그러나 FRC에서 반환 한 데이터가없고 핵심 데이터가 엔티티를 반환하지 않는 경우 문제가 발생합니다. 이것은 지금까지 내 코드입니다

The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). with userInfo (null)

: 나는 다음과 같은 오류와 함께 다음, 응용 프로그램의 중단을 처음으로 관리 객체를 추가하려고하면 내가 뭔가를 잘못하고 있어요 확인을 위해

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if ([self.fetchedResultsController.fetchedObjects count] == 0) { 
     return 1; 
    } 
    return [[self.fetchedResultsController sections] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 

    if ([self.fetchedResultsController.fetchedObjects count] == 0) { 
     return 1; 
    } 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 

    return [sectionInfo numberOfObjects] + 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if ([self.fetchedResultsController.fetchedObjects count] == 0) { 

     static NSString *identificadorCelda2 = @"CeldaNuevoGasto"; 
     UITableViewCell *celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2]; 
     if (celda == nil) { 
      celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2]; 
     } 
     return celda; 
    } 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:indexPath.section]; 

    NSInteger nsection = [sectionInfo numberOfObjects]; 

    if (indexPath.row == nsection) { 

     static NSString *identificadorCelda2 = @"CeldaNuevoGasto"; 
     UITableViewCell *celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2]; 
     if (celda == nil) { 
      celda = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identificadorCelda2]; 
     }   
     return celda; 
    } 

    static NSString *CellIdentifier = @"CeldaGasto"; 

    CeldaGastos *cell = (CeldaGastos *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = (CeldaGastos *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    } ..... 

,하지만 난 그것을 이해할 수 없다. 추가에 응답하려면 NSFetchedResultsController 대리자 메서드를 수정해야합니까? 아니면 뭔가 easiser입니다. 감사합니다;)

업데이트

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 

    NSInteger nsection = [self.fetchedResultsController.sections count]; 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      if (nsection == 1 && [sectionInfo numberOfObjects] == 1) { 
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 

      } 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 

      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 

      if (!nsection) 
       [self.tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

답변

1

내가 제대로 상황을 이해한다면, 당신은 참으로뿐만 아니라 NSFetchedResultsController 위임 방법을 변경해야합니다.

일부 NSFetchedResultsController 대리자 콜백에서 테이블보기의 -insertSections:withRowAnimation: 메서드를 사용하여 새 섹션을 테이블보기에 삽입 하시겠습니까?

나는 당신의 NSFetchedResultsControllerDelegate의 구현을 정확히 알고 있지만, 예를 들어 단계로이 상황 단계를 고려하지 않는다 :

  1. 는 처음에 당신이 어떤 개체를 가져올해야하고 -numberOfSectionsInTableView: 방법은 당신의 '추가'에 대한 1을 반환합니다 열. 당신이 엔티티와 NSFetchedResultsController의 대리인이 당신이 -insertSections:withRowAnimation:

  2. 테이블보기를 사용하여 테이블보기에 새 섹션을 삽입의 -controller:didChangeSection:atIndex:forChangeType: 콜백을 수신을 삽입 한 후

  3. 는 기대 -numberOfSectionsInTableView: 메소드를 호출하여 데이터 소스를 요청 1 개의 새 섹션을 삽입하기 때문에 1만큼 증가 된 값을 재회하십시오. 그러나 'add'행이 더 이상 고려되지 않기 때문에이 메소드는 1을 리턴합니다.

그러면 오류가 발생하게됩니다.

'추가'행을 제거하려면 더 이상 필요하지 않은 경우 -deleteSections:withRowAnimation:을 호출하면 문제가 해결 될 것입니다.

+0

도움을 주신 wanderwaltz에게 감사드립니다. 매우 도움이되었습니다. 위의 업데이트에서 내 메서드를 살펴 본다면 내가 말하는 것을 볼 수 있습니다.이것은 오류입니다. *** - [__ NSArrayM objectAtIndex :] : indexIn은 null을 userInfo (null)로 빈 배열에 대해 경계를 넘습니다. 직접 답변을 찾으려고 노력하지만 도움을 주시면 감사하겠습니다. – sam80

+0

테이블 뷰의 마지막 (그리고 유일한) 섹션을 삭제할 때 또는 여러 섹션이 있고 이들 중 하나가 삭제 된 경우 새 오류가 나타 납니까? 확실하지는 않지만, 예를 들어 2 섹션이 있고 첫 번째 섹션을 삭제하려고하면 (인덱스 0을 가짐), 업데이트 된 코드는이 섹션을 두 번 삭제하려고 시도합니다 ('if (sectionIndex == 0) '조건은이 경우 true입니다. 어쩌면 섹션 인덱스를 확인하지 않는 것이 좋지만 '추가'행을 추가/삭제할 때 섹션의 실제 총 수는 무엇입니까? –

+0

didChangeSection 메서드를 최신 버전으로 업데이트했습니다. pls가 있습니다;). 그것은 하나의 섹션과 1 행을 갖는 경우를 제외하고는 훌륭하게 작동합니다. (오류로 인해 깨지는 곳 : - [__ NSArrayM objectAtIndex :] : userInfo (null)로 공백 배열의 범위를 벗어나는 인덱스 0 – sam80

관련 문제