From ab0c357216564967ccbb900923ead3830a13752b Mon Sep 17 00:00:00 2001 From: matt handler Date: Mon, 25 Apr 2011 02:21:36 -0400 Subject: added json stuff, sources are loading but not connected to any sensical database or nothing... coming together. next hard bit is going to be the animation when things update --- Classes/SourcesEditViewController.m | 135 ++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 28 deletions(-) (limited to 'Classes/SourcesEditViewController.m') diff --git a/Classes/SourcesEditViewController.m b/Classes/SourcesEditViewController.m index d0ca382..93007a9 100644 --- a/Classes/SourcesEditViewController.m +++ b/Classes/SourcesEditViewController.m @@ -8,7 +8,8 @@ #import "SourcesEditViewController.h" #import "SourcesEditViewCell.h" - +#import "CJSONDeserializer.h" +#import "PicCastAppDelegate.h" @implementation SourcesEditViewController @@ -21,13 +22,29 @@ - (void)viewDidLoad { [super viewDidLoad]; - self.tableView.rowHeight = 100; + self.tableView.rowHeight = 44; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; //NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; - self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; + self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; + + sectionDictionary = [[[SectionDictionary alloc] init] retain]; + + _receivedData = [[NSMutableData data] retain]; + NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://piccast.memeschemes.com/json/feeds/"] + cachePolicy:NSURLRequestUseProtocolCachePolicy + timeoutInterval:30.0]; + + if ([NSURLConnection connectionWithRequest:theRequest delegate:self]) { + [_receivedData setLength:0]; + } else { + //[MBProgressHUD hideHUDForView:self.view animated:YES]; + [PicCastAppDelegate prompt:@"Error" withMessage:@"No internet connection" andButtonTitle:@"shucks" withDelegate:self]; + } + + //[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"haveSearched"]; } - (IBAction)done:(id)sender { @@ -67,50 +84,110 @@ } */ +#pragma mark - +#pragma mark url response + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + [_receivedData setLength:0]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { + [_receivedData appendData:data]; +} + +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + + //[_receivedData release]; + + //[MBProgressHUD hideHUDForView:self.view animated:YES]; + [PicCastAppDelegate prompt:@"Error" withMessage:[error localizedDescription] andButtonTitle:@"Aww man..." withDelegate:self]; + //[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); + +} + +- (void)connectionDidFinishLoading:(NSURLConnection *)connection { +// NSString *json = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding]; +// [_tableData removeAllObjects]; +// [_tableData addObjectsFromArray:[[json JSONValue] objectForKey:@"results"]]; +// [json release]; +// +// if ([_tableData count] > 0) { +// [tableView reloadData]; +// [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; +// self.tableView.allowsSelection = YES; +// self.tableView.scrollEnabled = YES; +// } + + //[MBProgressHUD hideHUDForView:self.view animated:YES]; + + //[_receivedData release]; + + NSError *theError = nil; + id array = [[CJSONDeserializer deserializer] deserialize:(NSData *)_receivedData error:&theError]; + //NSLog(@"class: %@", [array class]); + for (NSDictionary *obj in array) { + [sectionDictionary appendObject:obj forKey:@"Potpourri"]; + } + + [tableView reloadData]; +} #pragma mark - #pragma mark Table view data source -//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { -// // Return the number of sections. -// return <#number of sections#>; -//} +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + // Return the number of sections. + return [sectionDictionary count]; +} // // - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. - return 10; + return [[sectionDictionary objectForIndex:section] count]; +} + +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { + return [sectionDictionary keyForIndex:section]; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - static NSString *CellIdentifier = @"SourcesEditCell"; + static NSString *CellIdentifier = @"cell"; - SourcesEditViewCell *cell = (SourcesEditViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { - NSArray *tmpCell = [[NSBundle mainBundle] loadNibNamed:@"TopicTableViewCell" owner:self options:nil]; - - for (id currentObj in tmpCell) { - if ([currentObj isKindOfClass:[TopicTableViewCell class]]) { - cell = (SourcesEditViewCell *)currentObj; - [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; - - UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; - [cell addSubview:mySwitch]; - cell.accessoryView = mySwitch; - - //cell.accessoryView = cell.switcher; - break; - } - } - //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; - } +// NSArray *tmpCell = [[NSBundle mainBundle] loadNibNamed:@"TopicTableViewCell" owner:self options:nil]; +// +// for (id currentObj in tmpCell) { +// if ([currentObj isKindOfClass:[TopicTableViewCell class]]) { +// cell = (TopicTableViewCell *)currentObj; +// [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; +// +// UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; +// [cell addSubview:mySwitch]; +// cell.accessoryView = mySwitch; +// +// //cell.accessoryView = cell.switcher; +// break; +// } +// } + cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; + } // Configure the cell... - + [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; + + + NSDictionary *obj = [[sectionDictionary objectForIndex:indexPath.section] objectAtIndex:indexPath.row]; + [cell setText:[[obj objectForKey:@"fields"] objectForKey:@"title"]]; + + UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; + [cell addSubview:mySwitch]; + cell.accessoryView = mySwitch; + return cell; } @@ -187,6 +264,8 @@ - (void)dealloc { + [sectionDictionary release]; + [_receivedData release]; [super dealloc]; } -- cgit v1.2.3