Skip to content

Instantly share code, notes, and snippets.

@xenda
Created August 8, 2012 15:36
Show Gist options
  • Save xenda/3296019 to your computer and use it in GitHub Desktop.
Save xenda/3296019 to your computer and use it in GitHub Desktop.
//
// EventsViewController.h
// Student Scene
//
// Created by Alvaro Pereyra Rabanal on 7/6/12.
// Copyright (c) 2012 Alvaro Pereyra Rabanal. All rights reserved.
//
#import "GroupedViewController.h"
#import "Event.h"
@interface EventsViewController : GroupedViewController
@property NSMutableArray *events;
@end
//
// EventsViewController.m
// Student Scene
//
// Created by Alvaro Pereyra Rabanal on 7/6/12.
// Copyright (c) 2012 Alvaro Pereyra Rabanal. All rights reserved.
//
#import "EventsViewController.h"
#import "Section.h"
#import "SectionHeaderCell.h"
#import "ASIHTTPRequest.h"
#import "EventHomeCell.h"
#import "MBProgressHUD.h"
@interface EventsViewController ()
@end
@implementation EventsViewController
@synthesize events;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.events = [[NSMutableArray alloc] initWithObjects:nil];
[self loadEvents];
[self createDetailsButton];
// Do any additional setup after loading the view.
}
- (void) loadEvents {
NSURL *authenticationURL = [NSURL URLWithString:@"http://test....."];
NSString *jsonParamsFormat = @"username=%@&password=%@";
NSString *json = [NSString stringWithFormat:jsonParamsFormat,
@"....", @"...."];
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:authenticationURL];
__weak ASIHTTPRequest *request = _request;
request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
[request setCompletionBlock:^{
NSLog(@"Request complete");
NSString *responseString = [request responseString];
responseString = [responseString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
[self parseEvents:responseString];
[self.groupedTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
[request setFailedBlock:^{
NSLog(@"Request err");
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request startAsynchronous];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading events...";
NSLog(@"Request sent");
}
- (void) parseEvents:(NSString *)token {
NSURL *eventsUrl = [NSURL URLWithString:@"http://test.....?pageNumber=1&pageSize=5"];
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:eventsUrl];
__weak ASIHTTPRequest *request = _request;
[request addRequestHeader:@"TOKEN_HEADER" value:token];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
self.events = [Event parseEventJSON:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSString *responseString = [request responseString];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request startAsynchronous];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0){
SectionHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableHeaderCell"];
[cell.title setFont:self.labelFont];
cell.title.text = [[self.sectionsBoolean[indexPath.section] title] uppercaseString];
cell.title.textColor = [self colorWithHexString:@"3d3d3d"];
return cell;
}
else {
UITableViewCell *cell = [self eventCell:tableView indexAt:indexPath.row - 1];
return cell;
}
}
- (UITableViewCell*) eventCell:(UITableView *)tableView indexAt:(int)index {
NSString *eventTitle = [[self.events objectAtIndex:index] title] ;
EventHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EventCell"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
}
else {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
//
// EventsViewController.m
// Student Scene
//
// Created by Alvaro Pereyra Rabanal on 7/6/12.
// Copyright (c) 2012 Alvaro Pereyra Rabanal. All rights reserved.
//
#import "EventsViewController.h"
#import "Section.h"
#import "SectionHeaderCell.h"
#import "ASIHTTPRequest.h"
#import "EventHomeCell.h"
#import "MBProgressHUD.h"
@interface EventsViewController ()
@end
@implementation EventsViewController
@synthesize events;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.events = [[NSMutableArray alloc] initWithObjects:nil];
[self loadEvents];
[self createDetailsButton];
// Do any additional setup after loading the view.
}
- (void) loadEvents {
NSURL *authenticationURL = [NSURL URLWithString:@"http://test....."];
NSString *jsonParamsFormat = @"username=%@&password=%@";
NSString *json = [NSString stringWithFormat:jsonParamsFormat,
@"....", @"...."];
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:authenticationURL];
__weak ASIHTTPRequest *request = _request;
request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
[request setCompletionBlock:^{
NSLog(@"Request complete");
NSString *responseString = [request responseString];
responseString = [responseString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
[self parseEvents:responseString];
[self.groupedTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}];
[request setFailedBlock:^{
NSLog(@"Request err");
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request startAsynchronous];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading events...";
NSLog(@"Request sent");
}
- (void) parseEvents:(NSString *)token {
NSURL *eventsUrl = [NSURL URLWithString:@"http://test.....?pageNumber=1&pageSize=5"];
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:eventsUrl];
__weak ASIHTTPRequest *request = _request;
[request addRequestHeader:@"TOKEN_HEADER" value:token];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
self.events = [Event parseEventJSON:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSString *responseString = [request responseString];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request startAsynchronous];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0){
SectionHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableHeaderCell"];
[cell.title setFont:self.labelFont];
cell.title.text = [[self.sectionsBoolean[indexPath.section] title] uppercaseString];
cell.title.textColor = [self colorWithHexString:@"3d3d3d"];
return cell;
}
else {
UITableViewCell *cell = [self eventCell:tableView indexAt:indexPath.row - 1];
return cell;
}
}
- (UITableViewCell*) eventCell:(UITableView *)tableView indexAt:(int)index {
NSString *eventTitle = [[self.events objectAtIndex:index] title] ;
EventHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EventCell"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
}
else {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
//
// GroupedViewController.m
// Student Scene
//
// Created by Alvaro Pereyra Rabanal on 7/30/12.
// Copyright (c) 2012 Alvaro Pereyra Rabanal. All rights reserved.
//
#import "GroupedViewController.h"
#import "Section.h"
#import "EventHomeCell.h"
#import "SectionHeaderCell.h"
@interface GroupedViewController ()
@end
@implementation GroupedViewController
@synthesize groupedTableView;
@synthesize sectionsBoolean;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self groupedTableView].backgroundColor = [UIColor clearColor];
[self groupedTableView].separatorStyle = UITableViewCellSeparatorStyleNone;
self.sectionsBoolean = [[NSMutableArray alloc] initWithCapacity:4];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sectionsBoolean.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self.sectionsBoolean[section] opened]) {
if ([self.sectionsBoolean[section] collection]) {
return [[self.sectionsBoolean[section] collection] count] + 1;
}
else {
return [self.sectionsBoolean[section] rowsPerSection] + 1;
}
} else {
///we just want the header cell
return 1;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0){
return HEADER_HEIGHT;
}
else {
return [self.sectionsBoolean[indexPath.section] rowHeightPerSection];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[self.sectionsBoolean[indexPath.section] setOpened:newValue];
[self.groupedTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
}
}
- (UITableViewCell*)headerCell:(UITableView*) tableView atIndex:(int)index{
SectionHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableHeaderCell"];
[cell.title setFont:self.labelFont];
cell.title.text = [[self.sectionsBoolean[index] title] uppercaseString];
cell.title.textColor = [self colorWithHexString:@"3d3d3d"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment