使用 XCode 4.6、iOS6、CoreData、UITableViewController、NSFetchesResultsController
我想出了如何将以下日期放入 NSDate 变量中:
todayDate
yesterdayDate
thisWeek
lastWeek
thisMonth
lastMonth
lastYear
现在我想使用 NSFetchedResultsController 根据上述变量中的数据将数据放入部分中。我假设我将不得不做一些日期比较:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}
有些用户可能没有某些部分。 因此,我需要有关在以下方法中添加什么来确定基于 fetchRequest 的部分数量的帮助:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
我还需要知道如何将数据划分为单元格numberOfRowsInSection:
并将其显示到单元格中cellforRowAtIndexPath:
一些代码示例会很好!谢谢。
编辑
我还没有集成控制器,但下面是要获取的代码:
- (void) refreshTable {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meeting" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastmoddate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) {
[self.refreshControl endRefreshing];
self.objects = results;
[self.tableView reloadData];
} onFailure:^(NSError *error) {
[self.refreshControl endRefreshing];
NSLog(@"An error %@, %@", error, [error userInfo]);
}];
这是我获取日期的方式:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];
[components setDay:([components day] - ([components weekday] - 1))];
NSDate *thisWeek = [cal dateFromComponents:components];
[components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components];
[components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];
[components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components];