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
当我运行我的应用程序时,我在日志中看到以下内容
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
问题 - 似乎提取数据是异步的,所以我得到最后一行
2014-06-19 09:51:57.900 yelp[15415:70b] businesses count (after fetch): 20
cellForRowAtIndexPath
已被调用且数据为nil
如何处理这种情况以确保在我们有数据时调用cellForRowAtIndexPath
?
[self.searchResultTableView reloadData];
,但不确定这是否做得对答案 0 :(得分:1)
cellForRowAtIndexPath
,因为您告诉表格有要显示的单元格。
此...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
应该......
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.businesses.count;
}