views:

41

answers:

1

HI Everyone,

It is my first post here and this is my problem: I am trying to get some data from a REST API call and show then in a UITableView.

This s what I am doing:

in the viewDidLoad: 1) here I initialize my array of things to show in the Table (that is empty at the beginning) 2) the table is loaded (with 0 rows) and 3) then the HTTP async call is issued.

Done this I do my stuff with the HTTP Response and when ready I call the reloadData on my table. Here the strange happens.

numberOfRowsInSelection returns me the correct number of rows but tableView:cellForRowAtIndexPath:indexPath for the indexPath.row always returns me zero! so no new row is added to the table.

  - (void)doJSONRequest{
    responseData = [[NSMutableData data] retain];

    NSString *addr = [[NSString alloc] initWithFormat:@"http://localhost:8080/blabla/v1/items?count=10"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [addr release];
}

- (void)doJSONRequestWithURL:(NSString *)url{
    responseData = [[NSMutableData data] retain];

    NSString *addr = [[NSString alloc] initWithFormat:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [addr release];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    //NSLog(@"[%@] connection:didReceiveResponse %@",[self class], response);
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //NSLog(@"[%@] connection:didReceiveData %@",[self class], data);
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"[%@]",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"NETWORK ERROR!"];
    [alert setMessage:@"App will close"];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"Close"];
    [alert show];
    [alert release];
    [alert show];
    [alert release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
    //NSLog(@"[%@] connectionDidFinishLoading %@",[self class], connection);
    [connection release];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];

    NSDictionary *res = [NSDictionary dictionaryWithDictionary:[responseString JSONValue]];

    NSDictionary *tmp = [res valueForKey:@"reviews"];
    tmp = [tmp valueForKey:@"reviews"];
    NSEnumerator *e = [tmp objectEnumerator];
    NSDictionary *tmp_review;

    int i=0;
    while (tmp_review = [e nextObject]) {
        //NSLog(@"tmp_review %@", tmp_review);
        //NSLog(@"count %d", i++);

            MyObject r = [... doing my staff...]
        [reviews addObject: r];
        [r release];
    };
    [reviewListTableView reloadData];
    [responseString release];

}

- (void)viewDidLoad {
//- (void)initUI {

    //review is the array where I put my data
    //it is empty at the beginning
    reviews = [[NSMutableArray alloc] initWithObjects:nil];
    [super viewDidLoad];
}

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

Sounds like you may not have properly implemented numberOfSectionsInTableView:. You should log indexPath.section in tableView:cellForRowAtIndexPath:indexPath to see what section value you are passing. You will have multiple indexPath.row values of zero because there is a zero row for each section.

TechZen
indexPath.section is always returning 0 since I have one section only...
0m4r