【发布时间】:2010-11-01 16:14:13
【问题描述】:
我正在为我的公司编写一个小型 iPhone 应用程序,该应用程序一次显示每个员工一周的预订情况。我正在使用核心数据来获取给定一周的“预订”列表,并希望将它们显示在 UITableView 中,该 UITableView 分解为一周中的每一天。
问题在于我需要为一周中的每一天显示 7 个部分(在部分/日期没有预订的地方显示“无预订”单元格)。
我有一个应用程序的屏幕截图here(抱歉,我是 StackOverlow 的新手,所以还不能发布图片)
目前,我通过使用“fetchResults”方法来实现这一点,该方法获取预订并将它们组织到一系列可能的日期中:
- (void)refetchResults {
// Drop bookings Array, replacing with new empty one
// 7 slots for 7 days each holding mutable array to recieve bookings where appropraite
self.bookings = [NSArray arrayWithObjects:[NSMutableArray array],
[NSMutableArray array], [NSMutableArray array],
[NSMutableArray array], [NSMutableArray array],
[NSMutableArray array], [NSMutableArray array], nil];
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Booking" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Limit to this weeks data
[fetchRequest setPredicate:
[NSPredicate predicateWithFormat:@"(date >= %@) && (date <= %@) && (resource == %@)",
firstDate,lastDate,resourceId]];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"recId" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Fetch records in to array
NSError *error;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (results == nil) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[fetchRequest release];
[sortDescriptor release];
[sortDescriptor2 release];
[sortDescriptors release];
// Walk through records and place in bookings Array as required
for (Booking *item in results) {
// Decide on array index by difference in firstDate and booking date
int idx = (int)[[item date] timeIntervalSinceDate:firstDate]/86400;
// Add the item to the approp MutArray
[(NSMutableArray *)[bookings objectAtIndex:idx] addObject:item];
}
// Reload table
[tableView reloadData];
}
我的问题是:有什么方法可以使用 NSFetchedResultsController 达到相同的结果?不知何故,我需要让 NSFetchedResultsController 有 7 个部分,一个用于一周中的每一天,其中一些可能没有预订。
非常感谢任何帮助:)
【问题讨论】:
标签: iphone objective-c core-data nsfetchedresultscontroller