Я получил результат с одним разделом. Я могу получить доступ к объектам, используя [[fetchedResultsController fetchedObjects] objectAtIndex: index]
для всех объектов. Но это не удается, когда я использую objectAtIndexPath следующим образом: [fetchedResultsController objectAtIndexpath: indexPath]
Ошибка возникает после того, как я вставляю строку (для одного из объектов SearchResult) в соответствующую таблицу. Кажется, что объект правильно вставлен в новую таблицу. После того, как я визуально это подтвердил, я выбираю одну из строк, и тогда начинается самое интересное.
Вот код, в котором возникает ошибка:
- (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];
}
У меня ошибка в последней строке.Журнал выглядит следующим образом:
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 я получаю исключение в последней строке, используя [fetchedResultsController objectAtIndexPath: indexPath]
. То же самое и с другими значениями индекса, но они всегда выглядят действительными.
*** Завершение работы приложения из-за неперехваченного исключения «NSInternalInconsistencyException», причина: «нет объекта с индексом 2 в разделе с индексом 0»
Итак, чтобы подвести итог, кажется, что существует правильное количество извлеченных объектов, там это один раздел (0), я могу получить доступ к каждому из них одним методом, но не другим. IndexPathForObject: всегда возвращает (null).
Это ошибка или я что-то не понимаю?
ОБНОВЛЕНИЕ
Вот код, реализующий методы протокола NSFetchedResultsControllerDelegate.
- (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;
}
}
ОБНОВЛЕНИЕ 2
Я не знаю, имеет ли это значение, но tableView инициализируется этой строкой:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped];
ОБНОВЛЕНИЕ 3
Когда я изменяю вызывающую ошибку строку следующим образом, она работает нормально. Но я бы предпочел сохранить такую же индексацию, как и в таблице.
//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath];
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]]