我有一个核心数据应用程序,它显示自定义对象列表,每个对象代表戏剧作品。
每个演出对象都有一系列属性 - 名称、 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/