-
-
Save kaspth/9461284 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Event.h" | |
#import "AFNetworking.h" // it does not depend of AFNetworking at the moment, so I'd delete it | |
@implementation Event | |
// change the id here to instancetype, read more here http://nshipster.com/instancetype/ | |
- (id)init | |
{ | |
return [self initWithTitle:@"defaultTitle" detail:@"defaultDetail"]; // don't need to assign self | |
} | |
- (id)initWithTitle:(NSString *)title detail:(NSString *)detail | |
{ | |
self = [super init]; | |
if (!self) return nil; | |
self.title = title; | |
self.detail = detail; | |
return self; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"resultsDay1": [ | |
{ | |
"title": "Registration(1st day)", | |
"detail": "Main Hallway" | |
}, | |
{ | |
"title": "Pre-Conference", | |
"detail": "3 sessions starting at 12:00PM" | |
}, | |
{ | |
"title": "Opening Keynote", | |
"detail": "Beautiful sun @5txc" | |
} | |
], | |
"resultsDay2": [ | |
{ | |
"title": "Registration(2nd day)", | |
"detail": "Main Hallway" | |
}, | |
{ | |
"title": "event TBD", | |
"detail": "Silver room" | |
}, | |
{ | |
"title": "event TBD 2", | |
"detail": "Copper room" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@class Event; // use a forward class declaration instead of importing header. This speeds up compile times. | |
@interface ScheduleTableViewController : UITableViewController | |
@property (strong, nonatomic) Event *event; // what does this event mean? Rename or remove. | |
//@property (strong, nonatomic) NSArray *eventData; // data suffix makes me think it's an NSData that I have to unpack | |
@property (strong, nonatomic) NSArray *events; // I'd just call it events instead. | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ScheduleTableViewController.h" | |
#import "AFNetworking.h" | |
@implementation ScheduleTableViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
NSURL *url = [[NSURL alloc] initWithString:@"http://backbone7.sdbapps.com/events2.json"]; | |
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
operation.responseSerializer = [AFJSONResponseSerializer serializer]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSMutableArray *events = [NSMutableArray array]; | |
for (NSString *key in responseObject) | |
for (NSArray *eventsForOneDay in responseObject[key]) | |
[events addObjectsFromArray:[self parseEventsForOneDay:eventsForOneDay]]; | |
self.events = [NSArray arrayWithArray:events]; | |
NSLog(@"The final JSON array is: %@", self.events); | |
[self.tableView reloadData]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Events!" | |
message:[error localizedDescription] | |
delegate:nil | |
cancelButtonTitle:@"Ok" | |
otherButtonTitles:nil]; | |
[alertView show]; | |
}]; | |
[operation start]; | |
} | |
#pragma mark - Table view data source | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; | |
//Event *event = self.eventData[indexPath.row]; // again, makes me think of NSData | |
Event *event = self.events[indexPath.row]; | |
cell.textLabel.text = event.title; | |
cell.detailTextLabel.text = event.detail; | |
return cell; | |
} | |
#pragma mark - Private | |
- (NSArray *)parseEventsForOneDay:(NSArray *)dayEvents | |
{ | |
NSMutableArray *events = [NSMutableArray array]; | |
for (NSDictionary *eventDict in dayEvents) { | |
Event *event = [[Event alloc] initWithTitle:eventDict[@"title"] detail:eventDict[@"detail"]]; | |
[events addObject:event]; | |
} | |
return [NSArray arrayWithArray:events]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment