Files
old-festivalhelper/FestivalHelper/schedule/FESScheduleTableViewController.m

174 lines
5.2 KiB
Objective-C

//
// FESScheduleTableViewController.m
// FestivalHelper
//
// Created by Hamo Hapic on 02/09/14.
// Copyright (c) 2014 Senad Uka. All rights reserved.
//
#import "FESScheduleTableViewController.h"
#import "FESSCheduleEntry.h"
#import "FESScheduleEntryCell.h"
@interface FESScheduleTableViewController ()
@end
@implementation FESScheduleTableViewController
@synthesize scheduleArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self getScheduleDataFromServer];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.scheduleArray count];
}
-(void)getScheduleDataFromServer {
NSLog(@"Getting data");
NSURL *url = [[NSURL alloc] initWithString:SCHEDULE_URL];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(connectionError) {
NSLog(@"Error getting %@ schedule data: - %@", SCHEDULE_URL, [connectionError localizedDescription]);
}
else {
NSLog(@"Parsing data.");
[self setupScheduleFromJSONArray:data];
[self.tableView reloadData];
}
}];
}
-(void)setupScheduleFromJSONArray:(NSData*)dataFromServerArray{
NSError *error;
self.scheduleArray = [[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.scheduleArray = [[NSMutableArray alloc] init];
for(NSDictionary *scheduleData in arrayFromServer)
{
FESSCheduleEntry *entry = [[FESSCheduleEntry alloc] initWithJSONData:scheduleData];
[self.scheduleArray addObject:entry];
}
NSLog(@"success!");
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *retrievedCell = [tableView dequeueReusableCellWithIdentifier:@"scheduleEntryCell" forIndexPath:indexPath];
FESScheduleEntryCell *cell = (FESScheduleEntryCell *)retrievedCell;
if(cell)
{
//The beauty of this is that you have all your data in one object and grab WHATEVER you like
//This way in the future you can add another field without doing much.
FESSCheduleEntry *scheduleEntry = [scheduleArray objectAtIndex:indexPath.row];
cell.scheduleDate.text = [scheduleEntry scheduleDate];
cell.scheduleDayOfWeek.text = [scheduleEntry scheduleDayOfWeek];
}
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
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end