开发者

NSURLConnection delegate and populating table view

开发者 https://www.devze.com 2023-02-07 16:19 出处:网络
I have to make a call to NSURLConnection and have the following delegate: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

I have to make a call to NSURLConnection and have the following delegate:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

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

    results = [NSArray array];
    results = [responseString JSONValue];
        NSLog(@"Number of rows: %d", [results count]);
}

//table view number of rows    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        //return [_userInfo.friendsInfo count];
        NSLog(@"Number of rows: %d", [results count]);
        return [results count];
    }

As you can see I tried to print out the number of entries in the array and I got 5 when inside the delegate for NSURLConnection and 0 inside the tableView numberOfRowsInSection. I would like t开发者_开发技巧o use the data that I got from the NSURLConnection to populate the table. How can I do this?


I see two potential problems:

  1. You are missing a retain. The line "results = [NSArray array];" is pointless, and you should retain the the JSONValue as "results = [[responseString JSONValue] retain];" (remember to release at some point!)
  2. Also, are you reloading the tableView after you get the data? You should do something like [table reloadData]; or [self.tableView reloadData]; depending on your class structure.


Are you retaining the array that results points to? I think you need to call setResults (if it is synthesized) or explicitly retain.

0

精彩评论

暂无评论...
验证码 换一张
取 消