確定這個應該很簡單。用於在Xcode中創建應用程序。我有一個視圖。該視圖有兩個表格視圖。即將發生的事件表視圖和expiredEventsTableView。我正在爲核心數據提取數據。該數據具有名稱「event」和nsDate「eventTimeStart」。我想從當前時間「nowDate」分割eventTimeStart NSDate數據。我想在將來顯示expiredEventsTableView和NSDates上的過去數據以顯示在即將發生的EviewsTableView上。我認爲我走在正確的軌道上,但在排序描述符區域出現問題。到目前爲止,我在這兩個tableviews上都有相同的日期。只是不知道如何分隔每個tableview的日期。從核心數據得到2個數組,它們由nsdate分開它們
@interface EventsViewController()
@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@end
@implementation EventsViewController
@synthesize upcomingEventsTableView;
@synthesize expiredEventsTableView;
-(NSManagedObjectContext*)managedObjectContext{
return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.upcomingEventsTableView reloadData];
[self.expiredEventsTableView reloadData];
//scrollview stuff
[super viewDidLayoutSubviews];
[self.scrollView layoutIfNeeded];
self.scrollView.contentSize=self.contentView.bounds.size;
NSError *error = nil;
if(![[self fetchedResultsController]performFetch:&error]){
NSLog(@"Error! %@", error);
abort();
}
}
//upcoming & EXPIRED events tableview
-(void)viewWillAppear:(BOOL)animated{
[self.upcomingEventsTableView reloadData];
[self.expiredEventsTableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
if (upcomingEventsTableView){
return 1;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (upcomingEventsTableView){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}else{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
//format the date
-(NSDate *)convertDateToDate:(NSDate *) date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *nowDate = [[NSDate alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"yyyy-MM-d H:m:s"];
NSString * strdate = [formatter stringFromDate:date];
nowDate = [formatter dateFromString:strdate];
return nowDate;
}
#pragma mark - fetched results controller section
-(NSFetchedResultsController*)fetchedResultsController {
if (_fetchedResultsController != nil){
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"eventTimeStart" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
fetchRequest.sortDescriptors = sortDescriptors;
if ([sortDescriptors earlierDate: nowDate] == sortDescriptors) {
NSLog(@"upcoming");
//create nsarray upcoming here and apply to tableview upcomingEventsTableView;
}else{
NSLog(@"expired");
//create nsarray upcoming here and apply to tableview expiredEventsTableView
}
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
#pragma Mark - fetched results controller delegates
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.upcomingEventsTableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.upcomingEventsTableView endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.upcomingEventsTableView; //creating a temporary placeholder;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:{
Event *changeEvent = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = changeEvent.eventName;
break;
}
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
@end
我實際上正在與此做出不同的方向。因爲所有的答案都想把事情分開。 –