ios - AFNetworking 2.0 parsed data not displaying in table view -
my issue: no data displaying in table.
below code being used retrieve json object array , parse using afnetworking 2.0. have uiviewcontroller placed tableview prototype cell has reuse identifier eventcell. want display title , image in every cell. no errors @ runtime.
nsdictionary+events.h
#import <foundation/foundation.h> @interface nsdictionary (events) - (nsstring *)eventimage; - (nsstring *)eventtitle; - (nsdate *)eventdate; - (nsnumber *)eventprice; - (nsnumber *)eventticketstotal; @end
nsdictionary+events.m
#import "nsdictionary+events.h" @implementation nsdictionary (events) - (nsstring *)eventimage { return self[@"event_image"]; } - (nsstring *)eventtitle { return self[@"event_title"]; } - (nsdate *)eventdate { // nsstring *datestr = self[@"date"]; // date = "2013-01-15"; return [nsdate date]; } - (nsnumber *)eventprice { nsstring *cc = self[@"event_price"]; nsnumber *n = @([cc intvalue]); return n; } - (nsnumber *)eventticketstotal{ nsstring *cc = self[@"event_tickets_total"]; nsnumber *n = @([cc intvalue]); return n; } @end
nsdictionary+events_package.h
#import <foundation/foundation.h> @interface nsdictionary (nsdictionary_events_package) -(nsarray *)upcomingweather; @end
nsdictionary+events_package.m
#import "nsdictionary+events_package.h" @implementation nsdictionary (nsdictionary_events_package) - (nsarray *)upcomingweather { nsdictionary *dict = self[@"data"]; return dict[@"events"]; } @end
tdviewcontroller.h
#import <uikit/uikit.h> #import "nsdictionary+events.h" @interface tdviewcontroller : uiviewcontroller <uitableviewdatasource, uitableviewdelegate> @property(nonatomic, retain) uitableview *tableview; @end
tdviewcontroller.m
#import "tdviewcontroller.h" #import "uiimageview+afnetworking.h" #import "tdsecondviewcontroller.h" #import "nsdictionary+events_package.h" static nsstring * const baseurlstring = @"http://xx.zzz.yy.xx/test/"; @interface tdviewcontroller () @property(strong) nsdictionary *events; @end @implementation tdviewcontroller - (void)viewdidload { [super viewdidload]; // 1 nsstring *string = [nsstring stringwithformat:@"%@event_json.php?format=json", baseurlstring]; nslog(@"web service address: %@", string); nsurl *url = [nsurl urlwithstring:string]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; // 2 afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request]; operation.responseserializer = [afjsonresponseserializer serializer]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { // 3 self.events = (nsdictionary *)responseobject; nslog(@"%@", responseobject); self.title = @"json retrieved"; [self.tableview reloaddata]; } failure:^(afhttprequestoperation *operation, nserror *error) { // 4 uialertview *alertview = [[uialertview alloc] initwithtitle:@"error retrieving events" message:[error localizeddescription] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alertview show]; }]; // 5 [operation start]; } - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if([segue.identifier isequaltostring:@"weatherdetailsegue"]){ uitableviewcell *cell = (uitableviewcell *)sender; nsindexpath *indexpath = [self.tableview indexpathforcell:cell]; tdsecondviewcontroller *wac = (tdsecondviewcontroller *)segue.destinationviewcontroller; nsdictionary *w; if (indexpath.section == 0) { { w = [self.events upcomingweather][indexpath.row]; //nslog( @"this w: %@", w ); } wac.weatherdictionary = w; } } } #pragma mark - table view data source - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if(!self.events) return 0; nsarray *upcomingweather = [self.events upcomingweather]; return [upcomingweather count]; nslog( @"the count %lu", (long)[upcomingweather count]); } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"eventcell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; nsdictionary *daysweather = nil; nsarray *upcomingweather = [self.events upcomingweather]; daysweather = upcomingweather[indexpath.row]; nslog( @"this daysweather: %@", daysweather ); cell.textlabel.text = [daysweather eventtitle]; nslog(@"the content of arry is%@",daysweather); nsurl *url = [nsurl urlwithstring:daysweather.eventimage]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; uiimage *placeholderimage = [uiimage imagenamed:@"placeholder"]; __weak uitableviewcell *weakcell = cell; [cell.imageview setimagewithurlrequest:request placeholderimage:placeholderimage success:^(nsurlrequest *request, nshttpurlresponse *response, uiimage *image) { weakcell.imageview.image = image; [weakcell setneedslayout]; } failure:nil]; return cell; } #pragma mark - table view delegate - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { // navigation logic may go here. create , push view controller. } @end
below nslog output when run project on simulator (note not of nslog statements displaying).
2014-04-06 21:47:04.592 iclub[9198:60b] webservice address: http://xx.zzz.yy.xx/test/event_json.php?format=json 2014-04-06 21:47:04.685 iclub[9198:60b] { data = { events = ( { "event_date" = "7-5-2014"; "event_id" = 22; "event_image" = "http://xx.zzz.yy.xx/..."; "event_price" = 150; "event_tickets_total" = 200; "event_title" = "new event title"; }, { "event_date" = "3-2-2014"; "event_id" = 23; "event_image" = "http://xx.zzz.yy.xx/..."; "event_price" = 150; "event_tickets_total" = 200; "event_title" = "new event title 2"; }, { "event_date" = "9-25-2014"; "event_id" = 24; "event_image" = "http://xx.zzz.yy.xx/..."; "event_price" = 150; "event_tickets_total" = 200; "event_title" = "new event title 33"; } ); }; }
i can't catch issue is, app builds , runs on simulator, cells appear empty. appreciate help. thanks, jt.
possible sources of problem include:
failure connect
iboutlet
table view. iftableview
property not hooked properly,nil
, ,[self.tableview reloaddata]
nothing (because sending messagenil
object nothing).frankly, i'm unclear how
tableview
property getting set, because lacks typicaliboutlet
qualification used when connecting controls in interface builder , don't see setting programmatically, either.failure specify view controller
delegate
and, more importantly,datasource
of table view. if case, if called[self.tableview reloaddata]
, not result inuitableviewdatasource
methods being called).
i'd suggest checking self.tableview
, self.tableview.datasource
non-nil
.
Comments
Post a Comment