ios - TableViewController: Async call, numberOfRowsInSection returns 0 -
ios - TableViewController: Async call, numberOfRowsInSection returns 0 -
i new ios programming first question listed tableviewcontroller info empty in cellforrowatindexpath
upon suggestions, made changes 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) { } 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"]; 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 = self.searchresulttableview; nslog(@"fetching businesses"); [self fetchbusinesses]; [self.searchresulttableview reloaddata]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { nslog(@"total rows: %d", self.businesses.count); homecoming self.businesses.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nslog(@"getting cell"); 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 logs
2014-06-19 10:12:23.580 yelp[16008:70b] fetching businesses 2014-06-19 10:12:23.602 yelp[16008:70b] total rows: 0 2014-06-19 10:12:24.026 yelp[16008:70b] businesses count (after fetch): 20 now since self.businesses.count 0, never calls cellforrowatindexpath, when async phone call returns self.businesses.count 20.
question - do numberofrowsinsection , subsequently cellforrowatindexpath called when async phone call [self.fetcbbusinesses] done?
thanks
1st - check ib
all these outlets must connected
2nd - add together "reloaddata" after line
nslog(@"businesses count (after fetch): %d", self.businesses.count); you code->
[self.client searchwithterm:@"thai" success:^(afhttprequestoperation *operation, id response) { self.businesses = response[@"businesses"]; nslog(@"businesses count (after fetch): %d", self.businesses.count); [self.searchresulttableview reloaddata]; } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", [error description]); }]; i hope it's help you. luck!
ios uitableview
Comments
Post a Comment