ios - TableViewController data is empty in cellForRowAtIndexPath -
ios - TableViewController data is empty in cellForRowAtIndexPath -
i new ios programming
i trying fetch info yelp's api , nowadays in uitableviewcell
my code looks @interface searchresultviewcontroller () @property(nonatomic, strong) yelpclient *client; @property(weak, nonatomic) iboutlet uitableview *searchresulttableview; @property(strong, nonatomic) nsarray *businesses; @end @implementation searchresultviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { nslog(@"fetching businesses"); [self fetchbusinesses]; } homecoming self; } - (void)fetchbusinesses { // can register yelp api keys here: http://www.yelp.com/developers/manage_api_keys self.client = [[yelpclient alloc] initwithconsumerkey:kyelpconsumerkey
consumersecret:kyelpconsumersecret accesstoken:kyelptoken accesssecret:kyelptokensecret];
[self.client searchwithterm:@"thai" success:^(afhttprequestoperation *operation, id response) { self.businesses = response[@"businesses"]; [self.searchresulttableview reloaddata]; nslog(@"businesses count (after fetch): %d", self.businesses.count); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", [error description]); }]; } - (void)viewdidload { [super viewdidload]; self.searchresulttableview.datasource = self; self.searchresulttableview.delegate = self; uitableviewcontroller *uitableviewcontroller = [[uitableviewcontroller alloc] init]; uitableviewcontroller.tableview = _searchresulttableview; nslog(@"view loaded"); } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming 5; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { restaurantviewcell *restaurantviewcell = [tableview dequeuereusablecellwithidentifier:@"restaurantviewcell"]; if (restaurantviewcell == nil) { restaurantviewcell = [[restaurantviewcell alloc] initwithstyle:uitableviewcellstyledefault
reuseidentifier:@"restaurantviewcell"]; }
nslog(@"businesses count (for cell view): %d", self.businesses.count); restaurantviewcell.textlabel.text = @"osha's thai"; homecoming restaurantviewcell; } @end
when run application, see next in log
2014-06-19 09:51:57.447 yelp[15415:70b] fetching businesses 2014-06-19 09:51:57.483 yelp[15415:70b] view loaded 2014-06-19 09:51:57.485 yelp[15415:70b] businesses count (for cell view): 0 2014-06-19 09:51:57.486 yelp[15415:70b] businesses count (for cell view): 0 2014-06-19 09:51:57.487 yelp[15415:70b] businesses count (for cell view): 0 2014-06-19 09:51:57.487 yelp[15415:70b] businesses count (for cell view): 0 2014-06-19 09:51:57.487 yelp[15415:70b] businesses count (for cell view): 0 2014-06-19 09:51:57.900 yelp[15415:70b] businesses count (after fetch): 20
questions - seems fetching info asynchronous, lastly line
2014-06-19 09:51:57.900 yelp[15415:70b] businesses count (after fetch): 20
but meanwhile cellforrowatindexpath
called , info nil
how handle situation create sure cellforrowatindexpath
called when have data?
[self.searchresulttableview reloaddata];
not sure if doing right thing
cellforrowatindexpath
beingness called because told table there cells display.
this...
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming 5; }
should be...
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming self.businesses.count; }
ios objective-c uitableview
Comments
Post a Comment