Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created January 15, 2014 13:27
Show Gist options
  • Save Cellane/8436171 to your computer and use it in GitHub Desktop.
Save Cellane/8436171 to your computer and use it in GitHub Desktop.
//
// ClassroomViewController.m
// Univerzita
//
// Created by Milan Vít on 12.01.14.
// Copyright (c) 2014 Robert Jarusek. All rights reserved.
//
#import "ClassroomViewController.h"
#define BACKGROUND_COLOR [UIColor colorWithRed:89/255.0 green:138/255.0 blue:176/255.0 alpha:1.0]
@interface ClassroomViewController ()
{
CHCSVParser *csvParser;
NSMutableArray *entries;
NSMutableData *responseData;
NSMutableDictionary *item;
NSMutableString *roomName;
NSMutableString *roomInstructions;
}
@end
@implementation ClassroomViewController
#pragma mark Superclass methods
- (void)viewDidLoad
{
[super viewDidLoad];
// TODO: uncomment when .xib is ready
//[[self tableView] registerNib:[UINib nibWithNibName:@"ClassroomTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CommentCell"];
[[self view] setBackgroundColor:BACKGROUND_COLOR];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cl.ly/TK6l/ucebny.csv"]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// TODO: change this when you are sure about data structure
entries = [[NSMutableArray alloc] init];
[connection start];
[self setTitle:@"Rozmístění učeben"];
}
#pragma mark UITableViewData delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [entries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CommentCell";
// TODO: change when .xib is ready
// Can we reuse NewsViewController? Probably not
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setText:[[entries objectAtIndex:(NSUInteger) [indexPath row]] objectForKey:@"roomName"]];
[[cell detailTextLabel] setText:[[entries objectAtIndex:(NSUInteger) [indexPath row]] objectForKey:@"roomInstructions"]];
return cell;
}
#pragma mark UITableView delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Někdo na mě šáhl a já nevím, jak si to vyložit!");
}
#pragma mark NSURLConnectionData delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *csvString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
entries = [[NSMutableArray alloc] init];
csvParser = [[CHCSVParser alloc] initWithCSVString:csvString];
[csvParser setDelegate:self];
[csvParser parse];
}
#pragma mark CHCSVParser delegate methods
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber
{
item = [[NSMutableDictionary alloc] init];
roomName = [[NSMutableString alloc] init];
roomInstructions = [[NSMutableString alloc] init];
}
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber
{
[item setObject:roomName forKey:@"roomName"];
[item setObject:roomInstructions forKey:@"roomInstructions"];
[entries addObject:[item copy]];
}
- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex
{
switch (fieldIndex)
{
case 0:
[roomName setString:field];
break;
case 1:
[roomInstructions setString:field];
break;
default:
NSLog(@"This was not supposed to happen, ever. How did you manage to break the app this well? Grats!");
break;
}
}
- (void)parserDidEndDocument:(CHCSVParser *)parser
{
[[self tableView] reloadData];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment