获取异步JSON数据后重新加载tableview

时间:2012-04-23 02:23:46

标签: iphone ios xcode uitableview reloaddata

我使用异步JSON请求和响应来获取一些数据。我想在tableview中填充它。但是,我看到的只是空白表格单元格。我尝试了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];

}

tableview看起来像这样:

- (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甚至没有提示我。

谢谢。

1 个答案:

答案 0 :(得分:0)

也许你错过了这一行:

storeDetailsTable.delegate=self;