Skip to content

Instantly share code, notes, and snippets.

@jankeesvw
Created June 5, 2012 07:00
Show Gist options
  • Save jankeesvw/2873194 to your computer and use it in GitHub Desktop.
Save jankeesvw/2873194 to your computer and use it in GitHub Desktop.
//
// WLWatchListViewController.m
// WatchList
//
// Created by Jankees Woezik on 04-06-12.
// Copyright (c) 2012 Twelve Twenty. All rights reserved.
//
#import "WLWatchListViewController.h"
#import "WLModel.h"
#import "Movie.h"
#import "WLWatchList.h"
@interface WLWatchListViewController ()
@property(nonatomic, retain) NSManagedObjectModel *moviesDatabase;
@end
@implementation WLWatchListViewController
@synthesize moviesDatabase = _moviesDatabase;
@synthesize fetchedResultsController = _fetchedResultsController;
- (id)init
{
self = [super init];
if (self)
{
self.title = @"Watchlist";
self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}
- (void)loadView
{
[super loadView];
WLWatchList *tableView = [[WLWatchList alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tableView.dataSource = self;
tableView.delegate = self;
self.view = tableView;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)viewDidLoad
{
NSError *error;
[[self fetchedResultsController] performFetch:&error];
if (error)
{
ELog(@"Error fetching rows: %@", error.description);
}
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
ILog(@"numberOfSectionsInTableView: %i", [[self.fetchedResultsController sections] count]);
return [[self.fetchedResultsController sections] count];;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
ILog(@"titleForHeaderInSection section: %i", section);
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
ILog(@"sectionIndexTitlesForTableView: %@", self.tableView);
return [self.fetchedResultsController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
ILog(@"sectionForSectionIndexTitle title: %@", title);
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ILog(@"cell for row at indexpath");
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"WLWatchListCell"];
Movie *movie = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = movie.movieTitle;
cell.detailTextLabel.text = @"sub title";
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.moviesDatabase)
{
self.moviesDatabase = [[WLModel instance] managedObjectModel];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:[Movie entityName] inManagedObjectContext:[[WLModel instance] mainContext]]];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"movieTitle" ascending:NO]]];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[[WLModel instance] mainContext]
sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment