Jim*_*Jim 8 core-data objective-c uitableview nsfetchedresultscontroller
我有一个部分的获取结果.我可以使用[[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];
}
Run Code Online (Sandbox Code Playgroud)
我在最后一行失败了.日志看起来像这样:
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]
Run Code Online (Sandbox Code Playgroud)
执行NSLog语句后,我在最后一行使用了一个异常[fetchedResultsController objectAtIndexPath:indexPath].它也适用于其他索引值,但它们总是看起来有效.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'
因此,总而言之,似乎有正确数量的获取对象,有一个部分(0),我可以通过一个方法访问每个,但不能通过另一个方法访问.indexPathForObject:始终返回(null).
这是一个错误还是我误解了什么?
UPDATE
这是代码,实现了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;
}
}
Run Code Online (Sandbox Code Playgroud)
更新2
我不知道它是否重要,但tableView是用这一行初始化的:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped];
Run Code Online (Sandbox Code Playgroud)
更新3
当我按如下方式改变违规行时,它工作正常.但我宁愿保持索引与表格相同.
//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath];
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7075 次 |
| 最近记录: |