在获取异步 JSON 数据后重新加载表视图



我使用异步JSON请求和响应来获取一些数据。我想在表格视图中填充它。但是,我看到的只是空白的表格单元格。我尝试做一个reloadData但它不起作用。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://strong-earth-32.heroku.com/stores.aspx"]];//asynchronous call
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    resultData = [[parser objectWithString:responseString error:nil]copy];

    NSArray *storeArray = [[NSArray alloc]init];
    storeArray= [resultData objectForKey:@"stores"];
    populatedStoreArray = [[NSMutableArray alloc]init];
    images = [[NSMutableArray alloc] init];
...........
[populatedStoreArray addObject:tempStore];
    } 
    [self.storeDetailsTable reloadData];
        [parser release];
}

表视图如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    StoreCell *cell = (StoreCell *) [tableView dequeueReusableCellWithIdentifier:@"StoreCell"];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StoreCellView" owner:nil options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (StoreCell *) currentObject;
                break;
            }
        }
    }
    Store *storeAtIndex = [populatedStoreArray objectAtIndex:[indexPath row]];
    cell.storePhoneNumber.text = [storeAtIndex phone];
    cell.storeAddress.text = [storeAtIndex address];
    cell.storeImage.image = [images objectAtIndex:[indexPath row]];
    return cell;
}

我的更多代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [populatedStoreArray count];
}

头文件:

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *storeDetailsTable;
}
@property (nonatomic, retain) IBOutlet UITableView *storeDetailsTable;
@property (nonatomic, strong) NSDictionary *resultData;
@property (nonatomic, strong) NSMutableArray *populatedStoreArray;
@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, strong) NSMutableData *responseData;

@end

任何指针都将不胜感激,因为无论我搜索到哪里,我都被告知使用不起作用的reloadData。事实上,xcode 在我使用它时甚至没有提示我。

谢谢。

也许你错过了这一行:

storeDetailsTable.delegate=self;

最新更新