0

I have used asynchronous JSON request and response to get some data. I want to populate it in a tableview. However, all I see is blank table cells. I tried doing a reloadData but it is not working. Here is my code:

- (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];

}

The tableview looks like this:

- (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;
}

Some more of my code:

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

}

The header file:

@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

Any pointers will be appreciated because everywhere I search, I'm told to use reloadData which is not working. In fact, xcode is not even prompting me when I use it.

Thank you.

Shwethascar
  • 1,142
  • 9
  • 18
  • Did you implement the methods about `dataSource` of tableView? Could you show more codes about your implementations? – AechoLiu Apr 23 '12 at 02:26
  • I found out what was going wrong and it turns out that reloadData only reloads the cells which are already there. I would like to reload the table with more cells than were there before. How would this be possible ? – Shwethascar Apr 23 '12 at 02:42
  • It reloads all the rows you tell it exist when you answer numberOfRowsInSection:. That method should answer the count of your populatedStoreArray. – danh Apr 23 '12 at 02:43
  • that is true. The problem is, I have added more data into populatedStoreArray when I reload the table view. So, the count needs to be updated. How do I accomplish this ? – Shwethascar Apr 23 '12 at 02:48
  • Use this tutorial with sample code..>>> http://www.switchonthecode.com/tutorials/iphone-tutorial-searching-the-web-with-the-bing-sdk – Nitin Apr 23 '12 at 02:51
  • I followed the steps in the tutorial. Since, I;m checking if populatedStoreArray is nil and returning 0, the cells are still empty. However, I found out that even if I'm not specifying the count. So, only the first time numberofRowsInSection is being called. After that, the reload does not have a reference to count. So, I need to somehow call those functions again when I reload the data. – Shwethascar Apr 23 '12 at 03:13
  • 1
    is storeDetailsTable tableview is correctly linked to your iboutlet? – rishi Apr 23 '12 at 04:17
  • That was the problem. My storeDetailsTable was not linked to the IBOutlet. Thanks everyone. I linked it in the IB. Its working perfectly well now. – Shwethascar Apr 23 '12 at 04:20

1 Answers1

0

Maybe you missed this line:

storeDetailsTable.delegate=self;

Mil0R3
  • 3,876
  • 4
  • 33
  • 61