views:

286

answers:

2

Hello all working on my building my first iDevice app and I am trying to populate a UITableView with data from json result I can get it to load from plist array no propblem and I can even see my json the problem I'm having is that the UITableView never gets to see the json results please bare with me as this is first time with obj-c or anything like it

in my .h file

@interface TableViewViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    NSArray *exercises;
    NSMutableData *responseData;

}

in my .m file

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  exercises.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //create a cell
    UITableViewCell *cell = [[UITableViewCell alloc]
    initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:@"cell"];

    // fill it with contnets
    cell.textLabel.text = [exercises objectAtIndex:indexPath.row];
    // return it
    return cell;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {       
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://url_to_json"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self ];


    // load from plist
    //NSString *myfile = [[NSBundle mainBundle] pathForResource:@"exercise" ofType:@"plist"];
    //exercises = [[NSArray alloc] initWithContentsOfFile:myfile];
    [super viewDidLoad];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *dictionary = [responseString JSONValue];
    NSArray *response = [dictionary objectForKey:@"response"];

    exercises = [[NSArray alloc] initWithArray:response];
}
+2  A: 

You need to tell your table view to reload. Try adding:

[tableView reloadData];

at the end of your -connectionDidFinishLoading method.

Matt Long
I get error saying tableView is not delcared ?
mcgrailm
Okay. Is your view controller a UITableViewController or just a UIViewController? You will need an outlet to your UITableView. If your view controller is a UITableViewController, change the above code to [[self tableView] reloadData]; Otherwise, declare a UITableView IBOutlet in your header file and name it tableView and connect it in Interface Builder. Then the code should work.
Matt Long
ok got that part working thanks for your help
mcgrailm
A: 

use [self.tableView reloadData];

sanjeev