TableViewController数据在cellForRowAtIndexPath中为空

时间:2014-06-19 16:54:09

标签: ios objective-c uitableview

  • 我是iOS programming
  • 的新手
  • 我正在尝试从Yelp的API中获取数据并在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];,但不确定这是否做得对

1 个答案:

答案 0 :(得分:1)

正在调用

cellForRowAtIndexPath,因为您告诉表格有要显示的单元格。

此...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

应该......

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.businesses.count;
}