Reputation: 91969
iOS programming
UITableViewCell
@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]; } return self; } - (void)fetchBusinesses { // You can register for 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 { return 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"; return restaurantViewCell; } @end
When I run my application, I see the following 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 - It seems that fetching data is asynchronous, so I get last line as
2014-06-19 09:51:57.900 yelp[15415:70b] businesses count (after fetch): 20
cellForRowAtIndexPath
is already called and the data is nil
How do I handle this situation to make sure that cellForRowAtIndexPath
is called when we have data?
[self.searchResultTableView reloadData];
but not sure if that is doing right thingUpvotes: 0
Views: 161
Reputation: 4641
cellForRowAtIndexPath
is being called because you told the table that there are cells to display.
This...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
Should be...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.businesses.count;
}
Upvotes: 1