lundi 31 mars 2014

Data from JSON Parsing straight to UITableView


Vote count:

0




I am getting data using json with this code and I need to display it in a tableview with two part code and name the problem is writing it all to an array is taking forever and the array comes back null. How can I get each returned element as its own tableview cell? Hundreds of airports are returned.



NSString* path = @"http://ift.tt/1gijch1";


NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];

[_request setHTTPMethod:@"GET"];


NSURLResponse *response = nil;

NSError *error = nil;


NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];

if(nil != error)
{
NSLog(@"Error: %@", error);
}
else
{

NSMutableDictionary* json = nil;


if(nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}

if (error || !json)
{
NSLog(@"Could not parse loaded json with error:%@", error);
}
else
{


NSMutableDictionary *routeRes;

routeRes = [json objectForKey:@"airports"];




for(NSMutableDictionary *flight in routeRes)
{
NSLog(@"ident is %@", [flight objectForKey:@"name"]);
NSString *code=[json objectForKey:@"fs"];
NSString *name=[flight objectForKey:@"name"];
NSLog(@"code %@, name %@", code, name);


[candyArray addObject:[Candy code:code name:name]];

}



}

_connectionData = nil;
NSLog(@"connection done");
}


The following is the cellForRowatIndex were nothing is shown



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if ( cell == nil ) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }



// Create a new Candy Object
Candy *candy = nil;

// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
if (tableView == self.searchDisplayController.searchResultsTableView)
{
candy = [filteredCandyArray objectAtIndex:[indexPath row]];
}
else
{
candy = [candyArray objectAtIndex:[indexPath row]];
}

// Configure the cell
[[cell textLabel] setText:[candy name]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

return cell;


}



asked 52 secs ago






Aucun commentaire:

Enregistrer un commentaire