NSFetchedResultsController索引超出边界 [英] NSFetchedResultsController index beyond bounds

查看:116
本文介绍了NSFetchedResultsController索引超出边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NSFetchedResultsController在我的表视图中显示项:

   - (NSInteger)numberOfSectionsInTableView: )tableView {
//返回节的数量。
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//返回节中的行数。
return [[self.fetchedResultsController fetchedObjects] count];
}


//自定义表视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString * CellIdentifier = @TagCell

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease] ;;
}

Tag * tag = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = tag.name;

return cell;然而,此代码在此行中断开:











b

Tag * tag = [self.fetchedResultsController objectAtIndexPath:indexPath];



此消息:

  ***由于未捕获异常NSRangeException终止应用程序,原因:'***  -  [NSCFArray objectAtIndex :]:index(0)outside bounds(0)'



< c> [self.fetchedResultsController fetchedObjects]
,我可以确认确实有Tag对象。如果我替换上面的行,这一切都按预期工作:



Tag * tag = [[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row ];



I NSLogged indexPath ,索引为{0,0} section 0 row 0)所以我知道这不是一个问题。我非常困惑为什么这是发生,因为理论上,这两段代码做同样的事情。



感谢



更新: b
$ b

  id section = [[[self fetchedResultsController] sections] objectAtIndex:[indexPath section]]; 
NSLog(@Section%@,section); < - 返回有效的部分

此代码导致相同的异常: Tag * tag = [[section objects] objectAtIndex:[indexPath row];



如果I NSLog [section objects] 它返回一个空数组。我不知道为什么 [fetchedResultsController fetchedObjects] 返回一个具有正确对象的数组, [section objects] 返回没有。这是否意味着我创建的对象没有节?这里是我用来添加新对象的代码:

   - (void)newTagWithName:(NSString *)name 
{
NSIndexPath * currentSelection = [self.tableView indexPathForSelectedRow];
if(currentSelection!= nil){
[self.tableView deselectRowAtIndexPath:currentSelection animated:NO];
}

NSEntityDescription * entity = [[self.fetchedResultsController fetchRequest] entity];
Tag * newTag = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];

//配置新标签

newTag.name = name;

[self saveContext];

NSIndexPath * rowPath = [self.fetchedResultsController indexPathForObject:newTag];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:rowPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView selectRowAtIndexPath:rowPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

这是我的 saveContext 方法:

   - (void)saveContext 
{
//保存更改

NSError * error;
BOOL success = [self.managedObjectContext save:& error];
if(!success)
{
UIAlertView * errorAlert = [[[UIAlertView alloc] initWithTitle:@保存时遇到错误。 message:nil delegate:nil cancelButtonTitle:@DismissotherButtonTitles:nil] autorelease];
[errorAlert show];
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
}
}

我在这里做错了吗?

解决方案

我碰到了同样的问题。在我的情况下,它似乎是一个损坏的缓存文件。请尝试重命名缓存,或调用 deleteCacheWithName:


I'm using an NSFetchedResultsController to display items in my table view:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[self.fetchedResultsController fetchedObjects] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"TagCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];;
    }

 Tag *tag = [self.fetchedResultsController objectAtIndexPath:indexPath];
 cell.textLabel.text = tag.name;

    return cell;
}

However, this code breaks at this line:

Tag *tag = [self.fetchedResultsController objectAtIndexPath:indexPath];

With this message:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

I've NSLogged [self.fetchedResultsController fetchedObjects] and I can confirm that there are indeed Tag objects. If I replace that above line with this everything works as expected:

Tag *tag = [[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row];

I NSLogged indexPath and the indexes are {0, 0} (section 0 row 0) so I know it isn't an issue with the section. I'm extremely confused as to why this is happening because theoretically, those two pieces of code do the same thing. Any help is appreciated.

Thanks

UPDATES:

id section = [[[self fetchedResultsController] sections] objectAtIndex:[indexPath section]];
NSLog(@"Section %@", section); <-- returns a valid section

This code results in the same exception: Tag *tag = [[section objects] objectAtIndex:[indexPath row];

If I NSLog [section objects] it returns an empty array. I'm not sure why [fetchedResultsController fetchedObjects] returns an array with the right objects, and [section objects] returns nothing. Does this mean that the objects I'm creating have no section? Here's the code that I use to add new objects:

- (void)newTagWithName:(NSString *)name
{
    NSIndexPath *currentSelection = [self.tableView indexPathForSelectedRow];
    if (currentSelection != nil) {
        [self.tableView deselectRowAtIndexPath:currentSelection animated:NO];
    }    

    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    Tag *newTag = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];

    // Configure new tag

    newTag.name = name;

    [self saveContext];

    NSIndexPath *rowPath = [self.fetchedResultsController indexPathForObject:newTag];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:rowPath] withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView selectRowAtIndexPath:rowPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

And here's my saveContext method:

- (void)saveContext
{
    // Save changes

    NSError *error;
    BOOL success = [self.managedObjectContext save:&error];
    if (!success)
    {
        UIAlertView *errorAlert = [[[UIAlertView alloc] initWithTitle:@"Error encountered while saving." message:nil delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease];
        [errorAlert show];
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
}

Am I doing something wrong here?

解决方案

I ran into the same problem. In my case, it seems to be a corrupt cache file. Try renaming your cache, or calling deleteCacheWithName:.

这篇关于NSFetchedResultsController索引超出边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆