Files
old-festivalhelper/FestivalHelper/films/FESFilmsTableViewController.m
2014-09-07 07:18:07 +02:00

151 lines
4.2 KiB
Objective-C

//
// FESFilmsTableViewController.m
// FestivalHelper
//
// Created by Hamo Hapic on 06/09/14.
// Copyright (c) 2014 Senad Uka. All rights reserved.
//
#import "FESFilmsTableViewController.h"
#import "FESFilmEntry.h"
#import "FESDataProvider.h"
#import "FESFilmEntryCell.h"
#import "FESFilmDetailsViewController.h"
@interface FESFilmsTableViewController ()
@end
@implementation FESFilmsTableViewController
@synthesize filmsArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[FESDataProvider getDataFromServerForUrl:FILMS_URL andProcessThemWith:^(NSData *data) {
[self setupFilmsFromJSONArray:data];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}];
}
-(void)setupFilmsFromJSONArray:(NSData*)dataFromServerArray{
NSError *error;
self.filmsArray = [[NSMutableArray alloc] init];
NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromServerArray options:0 error:&error];
if(error){
NSLog(@"error parsing the json data from server with error description - %@", [error localizedDescription]);
}
else {
self.filmsArray = [[NSMutableArray alloc] init];
for(NSDictionary *filmData in arrayFromServer)
{
FESFilmEntry *entry = [[FESFilmEntry alloc] initWithJSONData:filmData];
[self.filmsArray addObject:entry];
}
NSLog(@"success!");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [filmsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *retrievedCell = [tableView dequeueReusableCellWithIdentifier:@"filmEntryCell" forIndexPath:indexPath];
FESFilmEntryCell *cell = (FESFilmEntryCell *)retrievedCell;
FESFilmEntry *film = [filmsArray objectAtIndex:indexPath.row];
cell.filmDirectors.text = film.filmDirectors;
cell.filmTitle.text = film.filmTitle;
cell.filmYear.text = film.filmYear;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
FESFilmDetailsViewController *destination = [segue destinationViewController];
destination.filmEntry = [filmsArray objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}
@end