ios - JSON parsing: show data on UITableView -
this given api have parse.i have show name on uitableview
. want fetch value of name n show on table view. code:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [ episodes count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"simpletableitem"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableidentifier]; } cell.textlabel.text = [[ episodes objectatindex:indexpath.row] objectforkey:@"names"]; return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uialertview *logoutconfirm = [[uialertview alloc]initwithtitle:@"toilet finder" message:@"" delegate:self cancelbuttontitle:nil otherbuttontitles:@"display on map",@"route",@"cancel", nil]; logoutconfirm.tag = 111; [logoutconfirm show]; } - (void) parsejsonwithurl:(nsurl *) jsonurl { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_background, 0); [uiapplication sharedapplication].networkactivityindicatorvisible = yes; dispatch_async(queue, ^{ nserror *error = nil; nsstring *json = [nsstring stringwithcontentsofurl:jsonurl encoding:nsasciistringencoding error:&error]; if (error == nil){ webinserve=@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"; nslog(@"url %@", webinserve); nsurl *final_url = [nsurl urlwithstring:webinserve]; nsdata* jsondata = [nsdata datawithcontentsofurl: final_url]; nserror* error; jsondict = [nsjsonserialization jsonobjectwithdata:jsondata options:kniloptions error:&error]; nslog(@"what contain let me check %@",jsondict); nsarray* latestloans = [jsondict objectforkey:@"loans"]; nslog(@"loans in array: %@", latestloans); episodes = [[jsondict valueforkey:@"latestloans"]valueforkey:@"name"]; if (error == nil) { dispatch_async(dispatch_get_main_queue(), ^{ [uiapplication sharedapplication].networkactivityindicatorvisible = no; episodes = [[jsondict valueforkey:@"latestloans"]valueforkey:@"name"]; [tableview reloaddata]; }); }
i have tried using:
episodes = [[jsondict valueforkey:@"latestloans"]objectforkey:@"name"];
i have used several tymes objectforkey
, valueforkey
in above code , in
cell.textlabel.text = [[ episodes objectatindex:indexpath.row] objectforkey:@"names"];
line of code in cellforrowatindexpath: method
but data not displayed on tableview...indeed displayed on nslog
...
why using dispatch_queues
instead of using dedicated async api of nsurlconnection
?
as explained in apple's concurrency programming guide, when there dedicated api execute task in concurrent manner, avoid make own concurrent version wrapping synchronous call (like +[nsstring stringwithcontentofurl:]
in case) inside asynchronous queues (like dispatch_async
on concurrent queue in case).
moreover, latestloans
variable (which nslog
) extracts content key called @"loans"
, in next line when try extract episodes, extract dictionary assumed associated @"latestloans"
key. there discrepancy in code there, isn't it? that's main problem in code.
and use valueforkey:
(which generic key-value-coding method works using introspection, not @ dedicated nsdictionary
-specific method) instead of objectforkey:
(which need use when accessing objects nsdictionary
).
sure valueforkey:
work objectforkey:
being dedicated method nsdictionary
more efficient. prefer new "objective-c modern notation" object literals, namely dict[key]
compiler translate objectforkey:
automatically (this []
notation syntaxic sugar more concise)
so here comes code suggestion:
... [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse* resp, nsdata* jsondata, nserror* err) { ... nsdictionary* jsondict = [nsjsonserialization jsonobjectwithdata:jsondata options:kniloptions error:&error]; nsarray* latestloans = jsondict[@"loans"]; self.episodes = latestloans[@"name"]; [self.tableview reloaddata]; }];
Comments
Post a Comment