Ok so I am thinking this might be a problem in IB but I figured I would post the code and at the very least it might help someone else out doing an asynchronous request or using NSDictionary
ViewController.h
Code:
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "AppDelegate.h"
@interface ViewController : UIViewController
{
NSMutableArray *photoTitles;
}
@property (nonatomic, strong) NSMutableArray *photoTitles;
@end
ViewController.m
Code:
#import "ViewController.h"
#import "SBJson.h"
@implementation ViewController
@synthesize photoTitles;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// [self setupArray];
[super viewDidLoad];
photoTitles = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://localhost/test.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIFormDataRequest *)request
{
NSString *responseString = [request responseString];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *photos = [[dictionary objectForKey:@"photos"] objectForKey:@"photo"];
for (NSDictionary *photo in photos) {
NSString *title = [photo objectForKey:@"title"];
[photoTitles addObject:title];
}
// Use when fetching text data
// Use when fetching binary data
NSData *responseData = [request responseData];
NSLog(@"Data: %@", responseData);
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [photoTitles count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [photoTitles objectAtIndex:row];
return cell;
}
- (void)requestFailed:(ASIFormDataRequest *)request
{
NSError *error = [request error];
NSLog(@"error: %@", error);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
This does not have any errors except the data from the photoTitles mutable array does not show up in the table view. Any help would be great.