8

Tek bir bölümle getirilmiş bir sonuç aldım. Tüm nesneler için [[fetchedResultsController fetchedObjects] objectAtIndex:index] kullanarak nesnelere erişebiliyorum. Ancak objectAtIndexPath'i şu şekilde kullandığımda başarısız oluyor: [fetchedResultsController objectAtIndexpath:indexPath]NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject tutarsızlıkları

Bir satırı (SearchResult nesnelerinden biri için) ilgili tabloya ekledikten sonra bir hata oluşur. Nesne yeni tablonun içine doğru şekilde yerleştirilmiş gibi görünüyor. Bunu görsel olarak doğruladıktan sonra, satırlardan birini seçiyorum ve sonra eğlence başlıyor.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SearchResult *event; 
    NSLog(@"Number of sections in fetchedResultsController: %d", [[fetchedResultsController sections] count]); 
    for (NSUInteger i=0; i<[[fetchedResultsController fetchedObjects] count]; i++) { 
     event = (SearchResult*)[[fetchedResultsController fetchedObjects] objectAtIndex:i]; 
     NSLog(@"object at index[%d]: %@", i, event.title); 
     NSLog(@"indexPath for object at index[%d]:%@", i, [fetchedResultsController indexPathForObject:event]); 
    } 

    NSLog(@"indexPath passed to method: %@", indexPath); 

    SearchResult *result = [fetchedResultsController objectAtIndexPath:indexPath]; // *** here is where the error occurs *** 

    [viewDelegate getDetails:result]; 
} 

ben son satırında bir başarısızlık yaşıyorum: Burada

hata oluşup kodudur. Günlük şuna benzer:

Number of sections in fetchedResultsController: 1 
object at index[0]: Cirles 
indexPath for object at index[0]:(null) 
object at index[1]: Begin 
indexPath for object at index[1]:(null) 
object at index[2]: Copy 
indexPath for object at index[2]:(null) 
object at index[3]: Unbound 
indexPath for object at index[3]:(null) 
indexPath passed to method: <NSIndexPath 0x64ddea0> 2 indexes [0, 2] 

NSLog ifadelerini yürütme sonra [fetchedResultsController objectAtIndexPath:indexPath] kullanarak son satırında bir istisna olsun. Diğer dizin değerleri için de geçerlidir, ancak her zaman geçerli görünürler. özetlemek Yani

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'

, zoraki nesnelerin doğru numara olması için orada görünüyor, bir (0), ben her bir yöntemdir biri erişebileceğiniz bölüm, ancak diğeriyle yoktur. IndexPathForObject: daima geri döner (boş).

Bu bir hata mıdır yoksa bir şeyi yanlış anlamış mıyım?


GÜNCELLEME İşte

NSFetchedResultsControllerDelegate protokol yöntemlerini uygulayan, kodudur.

- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    NSLog(@"Favorites controllerWillChangeContent"); 
    [self.tableView beginUpdates]; 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    NSLog(@"Favorites Table controllerDidChangeContent"); 
    [self.tableView endUpdates]; 
} 

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath 
    forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 

    NSLog(@"Favorites Changed Object"); 

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      NSLog(@"--- Favorite was inserted"); 
      if ([[self.fetchedResultsController fetchedObjects] count] == 1) { 
       // configure first cell to replace the empty list indicator by first deleting the "empty" row 
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      } 

      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 

      break; 

     case NSFetchedResultsChangeDelete: 
      NSLog(@"--- Favorite was deleted"); 

      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      if ([[self.fetchedResultsController fetchedObjects] count] == 0) { 
       // configure first cell to show that we have an empty list 
       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
      } 

      break; 

     case NSFetchedResultsChangeMove: 
      NSLog(@"--- Favorite was moved"); 

      break; 

     case NSFetchedResultsChangeUpdate: 
      NSLog(@"--- Favorite was updated"); 
      [self configureCell:(ResultCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 

      break; 

     default: 
      break; 
    } 

} 

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

    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      NSLog(@"Favorites Section Added"); 

      break; 

     case NSFetchedResultsChangeDelete: 
      NSLog(@"Favorites Section Deleted"); 

      break; 

     default: 
      break; 
    } 

} 

GÜNCELLEME 2

Bu konularda bilmiyorum ama tableView bu hat ile başlatılır:

tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped]; 

GÜNCELLEME 3

Hatalı çizgiyi aşağıdaki gibi değiştirdiğimde işe yarıyor iyi. Ancak indekslemeyi tablodakiyle aynı tutmayı tercih ederim.

//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]] 
+0

Nesneyi nasıl eklediniz? Nesneyi temel veri deponuza eklediğiniz kodu gönderebilir ve satırı tablonuza ekleyebilir misiniz? – timthetoolman

+0

Güncellendi. Yukarıyı görmek. – Jim

+0

Aslında Temel Veri Deposu'na bir şeyler mi ekliyorsunuz yoksa sadece tabloya bir satır ekliyor musunuz? – timthetoolman

cevap

0

Şimdi aynı sorunu yaşadım.

Benim için NSFetchedResultsController'un cacheName: nil ile başlatılmasına yardımcı oldu.

İlgili konular