iphone - 动态地将 UITableView 分成任意部分?

标签 iphone ios core-data ios4

我有一个核心数据应用程序,它显示自定义对象列表,每个对象代表戏剧作品。

每个演出对象都有一系列属性 - 名称、 Logo 、开幕日期、演出类型。

我可以很好地检索它们,并且可以很好地按类型排序 - 但我希望每个节目类型在表格中都是它自己的部分。

我事先不知道结果集中将表示多少节目类型,所以我一开始不知道 numberofsections 应该是多少。

所以我想问题是 - 我如何将结果集分成按类型分组的部分?

目前我正在这样做:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Show" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

最佳答案

我会编写 numberOfSections 方法,以便它动态检查类型的数量。然后,每当有新类型出现时,只需执行 [tableView reloadData];

关于iphone - 动态地将 UITableView 分成任意部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6005462/

相关文章:

c++ - 类 Hook - Mobile Substrate 返回值

core-data - 保存 ManagedObjectContext 时崩溃,值为 'NSInvalidArgumentException' ,但只是偶尔发生

ios - 我正在尝试为特定的 UIButton 调用 UIAlertView 方法,例如 UpdateEmail 或 ForgotPassword 但正确调用我正在使用此代码

ios - 身份验证成功后如何以编程方式继续?

ios - 在 iOS 4.3 中将 YpCbCr iPhone 4 相机帧渲染为 OpenGL ES 2.0 纹理

ios - 使用 Xcode 11 与 Xcode 10 编译时,NSData 描述和 NSString stringWithFormat 具有不同的返回结果

ios - 提高核心数据重复验证效率

iphone - 在应用程序中查看核心数据

ios - 当 segmentedControl 改变时在单元格之间切换

iphone - 在iPhone上访问用户的短信