TableViewController:异步调用,numberOfRowsInSection返回0

时间:2014-06-19 17:18:32

标签: ios uitableview

我是iOS programming的新手 我的第一个问题列在TableViewController data is empty in cellForRowAtIndexPath

根据建议,我对代码看似

进行了更改
@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) {

    }
    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"];

        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);
    return 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";
    return restaurantViewCell;
}
@end

当我跑步时,我将日志作为

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

由于self.businesses.count0,因此永远不会调用cellForRowAtIndexPath,但异步调用返回self.businesses.count时为20

问题
- 如果异步调用numberOfRowsInSection完成,我该怎么做才能调用cellForRowAtIndexPath和随后的[self.fetcBbusinesses]

由于

3 个答案:

答案 0 :(得分:0)

你走在正确的轨道上:

[self.searchResultTableView reloadData];应该照顾好这一点。

如果没有重新加载,则表示您的tableView未连接到属性searchResultTableView

答案 1 :(得分:0)

以下代码仅在fetchBusinesses同步时才有效。

[self fetchBusinesses];
[self.searchResultTableView reloadData];

但是,它不是同步的,这意味着您只应在收到回复后致电reloadData

- (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"];
        NSLog(@"businesses count (after fetch): %d", self.businesses.count);
        [self.searchResultsTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", [error description]);
    }];
}

答案 2 :(得分:0)

1 - 检查你的IB

必须连接所有这些插座

UITableView outlets

第二 - 添加" reloadData"在这一行之后

NSLog(@"businesses count (after fetch): %d", self.businesses.count);

您将获得代码 - >

 [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]);
}];

我希望它对你有所帮助。 祝你好运!