- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个属性为“组”的实体,因为我想按“组”(0 或 1)将我的实体列表分成两部分
@property (nonatomic, retain) NSNumber * group;
在我的 fetchedResultsController 中,我将 sectionNameKeyPath 指定为“group”
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"group" cacheName:nil];
如何在下面的方法中返回每个部分的行数?我收到以下错误:
Terminating app due to uncaught exception 'NSRangeException', reason:
'-[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'
代码如下:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section]
numberOfObjects];
}
我也试过这个,得到了同样的错误:
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]
objectAtIndex:section];
return [sectionInfo numberOfObjects];
请注意,我也实现了这个方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
最佳答案
您是否实现了numberOfSectionsInTableView:
?如果您不实现它,tableView 会假定您有 1 个部分,如果 fetchedResultsController 没有任何部分(即它没有对象),这将导致此异常。
您必须在 numberOfSectionsInTableView:
中返回 [sections count]
。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.fetchedResultsController.sections count];
}
如果你总是想显示两个部分,你必须检查 fetchedResultsController 是否有请求的部分。如果 fetchedResultsController 没有这个部分,不要向它询问此操作中的对象数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger count = 0;
NSInteger realNumberOfSections = [self.fetchedResultsController.sections count];
if (section < realNumberOfSections) {
// fetchedResultsController has this section
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
count = [sectionInfo numberOfObjects];
}
else {
// section not present in fetchedResultsController
count = 0; // for empty section, or 1 if you want to show a "no objects" cell.
}
return count;
}
如果您在 else 中返回 0 以外的值,您还必须更改 tableView:cellForRowAtIndexPath:
。与此方法类似,您必须检查 fetchedResultsController 是否在请求的 indexPath 处有一个对象。
关于ios - 如何使用 sectionNameKeyPath 从 NSFetchedResultsController 获取 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343346/
我想显示项目,以便它显示基于 sharedToUsers 的所有项目。表示带有 User.user_id 的部分名称,它应该显示与该用户的所有共享项目。但我无法正确设置 NSFetchedResult
我正在开发一个使用 Core Data 和 NSFetchedResultsController 的应用程序。模型设置是这样的: /-----------\ /-----------
我是核心数据方面的新手,正在尝试弄清楚如何在我的 NSFetchedResultsController 中创建自定义 sectionNameKeyPath。我有一个托管对象,其属性名为 acctPer
我在使用关系创建 tableView 部分时遇到困难。 我有两个具有关系的实体 List > Item。 我希望 List 成为部分,Item 成为行。我用键路径 @"itemList" 设置了 se
我有一个在 iOS 上使用 Core Data 数据源的 UITableViewController。这些项目是按日期排序的事件。我已经在使用 sectionNameKeyPath 按日期将事件分成几
我正在执行此 SO 帖子 Custom Core Data SectionNameKeyPath : 特别是 Martin R 的第一个建议是添加一个类别方法来喂我的 sectionNameKeyPa
我遵循了这个建议: Format sectionNameKeyPath NSFetchedResultsController - Swift 我在执行此操作时遇到了一些问题。使用 Xcode 8 和
抱歉核心数据新手... 我在 UITableViewController 上使用 NSFetchedResultsController。实体中的数据包含时间戳。作为 sectionID 我想在节标题中
我正在尝试使用存储的 NSDate 属性为我的核心数据提取格式化我的 sectionNameKeyPath。它完全正常工作,并根据 NSDate 将记录拉/分类到多个部分,但我不确定如何格式化它以按
我有一个实体 Order,其属性为 paid,这是一个 bool 值。 我想在 UITableView 中显示所有订单,但我想将它们分为两个部分:“未支付”和“已支付”。所以我想我应该将“付费”作为
我在 Core Data 中有一个任务列表。我使用 NSFetchedResultsController 将它们提取到 UITableView 中。 我需要自定义顺序的自定义部分: 逾期 活跃 进行中
假设有一个名为 testTable 的表: (NSNumber*) numValue (NSString*) stringValue 那么假设里面大概有50条记录。 numValue 每条记录都不一样
我有一个包含三个实体的核心数据模型:Notification、Group 和 Customer。它们之间的关系如下: 一个客户属于多个组,一个组可以有多个客户。 一个通知被发送(属于)一个组,一个组可
我有一个由 NSFetchedResultsController 支持的 UITableView。 表格 View 显示聊天对话。 我有几个聊天对话类型 (NSNumber),表格 View 根据您的
我有一种情况,我正在使用 NFC 获取消息列表。每条消息都有一个名为“未读”的属性,如果未被看到则设置为 true。我还使用了一个 sortDescriptor,它按照从最新到最旧的顺序给我消息。 我
情况是这样的。我有 2 个模型: 运动(具有属性名称和关系玩家) 播放器(具有属性名称和 relationShip sport) 在 ViewController 中,我不想在按运动排序的 UITab
我一直无法弄清楚如何在 Swift 中执行以下操作并寻找指针。 我正在从 Core Data 中提取字符串值并将它们显示在 UITableView 中……它正在工作并显示所有条目。我现在想按创建日期(
我有一个属性为“组”的实体,因为我想按“组”(0 或 1)将我的实体列表分成两部分 @property (nonatomic, retain) NSNumber * group; 在我的 fetche
使用 Xcode8.3 在 Swift 3 中工作良好 我正在进行一个项目,其中包含用于保存消息的核心数据。 它根据时间对消息进行排序,并根据日期对消息进行分段。 方法如下: let request
我正在使用 NSFetchedResultsController 按部分分组,其中 sectionNameKeyPath 根据类别。 我的类别是:成人、 child 、视觉、无。 升序排序:成人、 c
我是一名优秀的程序员,十分优秀!