Iphone NSFetchedResultsController索引超出边界
我正在使用NSFetchedResultsController在我的表视图中显示项目:Iphone NSFetchedResultsController索引超出边界,iphone,cocoa-touch,uitableview,core-data,nsfetchedresultscontroller,Iphone,Cocoa Touch,Uitableview,Core Data,Nsfetchedresultscontroller,我正在使用NSFetchedResultsController在我的表视图中显示项目: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)s
- (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;
}
但是,此代码在此行中断:
Tag*Tag=[self.fetchedResultsController对象索引路径:indexPath]代码>
谨此致辞:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
我已经记录了[self.fetchedResultsController fetchedObject]
,我可以确认确实存在标记对象。如果我将上面这一行替换为这一行,则一切都会按预期工作:
Tag*Tag=[[self.fetchedResultsController fetchedObject]objectAtIndex:indexPath.row]代码>
我记录了indepath
,索引是{0,0}(第0节第0行),所以我知道这不是该节的问题。我非常困惑为什么会发生这种情况,因为理论上,这两段代码做的是相同的事情。感谢您的帮助
谢谢
更新:
id section = [[[self fetchedResultsController] sections] objectAtIndex:[indexPath section]];
NSLog(@"Section %@", section); <-- returns a valid section
下面是我的saveContext
方法:
- (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]);
}
}
我在这里做错了什么吗?您是针对3.0进行编译、瞄准还是测试
您从以下代码中得到了什么:
id section = [[[self fetchedResultsController] sections] objectAtIndex:[indexPath section]];
NSLog(@"Section %@", section);
你能拿回有效的部分吗
如果是,请尝试添加以下行:
Tag *tag = [[section objects] objectAtIndex:[indexPath row];
如果这产生了一个错误,那么我怀疑问题出在您的-tableView:numberofrowsinssection:
中,它可能将错误的行数返回给UITableView
。你能编辑你的问题并发布该方法的代码吗
更新
在进一步的回顾中,我看到了TechZen所指的方向。您告诉表视图您有一个分区,而您可能没有。此外,您还可以告诉它整个NSFetchedResultsController
有多少个对象,这时您应该更简洁地回答它
这里是对您的代码的一个轻微更改
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
做出这些改变,看看有什么影响
问题:
您还实现了哪些UITableViewDataSource
方法?听起来您的实现中可能至少还有一个挥之不去的错误。当您初始化fetch request controller时,您给了它一个sectionNameKeyPath:
,即使您的数据没有节。然后您将表格部分编号硬编码为1
fetch请求控制器试图返回一个数据结构中节索引为零的对象,该数据结构中根本没有节
通过将sectionNameKeyPath从nil更改为实体唯一属性的名称,我在默认导航模板应用程序中重现了您的错误
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"timeStamp" cacheName:@"Root"];
。。。然后就变了
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return [[fetchedResultsController sections] count];
return 1;
}
我得到:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
设置部分名称keypath:nil
,这将解决您的问题 我遇到了同样的问题。在我的例子中,它似乎是一个损坏的缓存文件。尝试重命名缓存,或者调用deleteCacheWithName:
昨晚出现此错误,这让我抓狂。我发现,如果使用故事板指定行数和节数,则需要与数据源方法中指定的行数和节数相匹配。要完全修复此错误,可以在attributes inspector中将所有内容都设置为0,然后以编程方式全部执行,或者确保数据源方法和attributes inspector反映相同数量的行和节
^^我得到一个有效的分区,它在控制台中打印分区。我尝试添加该行,但它出现了相同的错误。我已经发布了上面的-tableView:numberofrowsinssection:
方法。此外,我正在编译/测试/瞄准3.2SDK。这让我很困惑,因为我正在使用同样的方法在另一个表视图中显示数据,而且效果很好。我已经用更多信息和代码更新了我的原始帖子。看来TechZen在看到您的扩展问题时就解决了这个问题。我做了上面提到的更改,现在没有异常或错误,但它也不显示任何对象。这是我分配抓取结果控制器的位置:`NSFetchedResultsController*aFetchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@“TagsCache”];`sectionNameKeyPath
被设置为nil。我被难住了。这显然是各部门和财务汇报委员会的问题。所有其他代码都是附带的。尝试将表中的节数更改为FRC直接返回的节数。将缓存也设置为nil。您还可以发布所有用于初始化FRC的代码。我想我将放弃使用NSFetchedResultsController,而使用我自己的类。无论如何,谢谢。祝你好运,但我不认为问题出在FRC本身。财务汇报委员会显然认为它有一些条款。代码中的某些东西告诉它。如果您的数据结构/控制代码中存在逻辑问题,您只需在复制FRC功能时重新创建它。您需要小心。同样,在初始化NSFetchedResultsController对象时,为缓存名称传递nil可以避免崩溃。叹气。将cachename设置为nil时,在ios 8和7.1.2上出现越界错误
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'