简体   繁体   中英

UITableView and Async loading with NSURLconnection

I'm having an issue which to me looks like my JSON URL request is completing after I load my UITableView. I attempted to reload the UITableview but that either isn't working properly or didn't do the trick as some of the other questions suggested. This viewcontroller is being coming off an AppDelegate. Would appreciate a steer if anyone sees something I'm not doing right.

#import "AppDelegate.h"
#import "ViewController.h"
#import "DetailViewController.h"
#import "JSON.h"

@implementation ViewController
@synthesize states,responseData,datasource,appDelegate,_tableView;
@synthesize allTeams;

#pragma mark - View lifecycle

- (void)viewDidLoad
{

    [self setupArray];
    [super viewDidLoad];

    // inside all teams
    NSLog(@"in VIEW allTeams count, %u",[allTeams count]);
// Do any additional setup after loading the view, typically from a nib.
}

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

    NSLog(@"Want to get teams for via the url");
    NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL     URLWithString:@"http://skyrink.secretagents.us/service/"]];

    [[NSURLConnection alloc]  initWithRequest:request delegate:self];

    }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSLog(@"datasource number of rows in section count, %u",[datasource count]);
    NSLog(@"allTeams number of rows in section count, %u",[allTeams count]);
    return [allTeams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...

    //---------- CELL BACKGROUND IMAGE -----------------------------
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
    UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
    imageView.image = image;
    cell.backgroundView = imageView;
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];

    //cell.textLabel.text = [datasource objectAtIndex:indexPath.row];

    //Arrow 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSLog(@"inside detail view");
    DetailViewController *detail = [self.storyboard     instantiateViewControllerWithIdentifier:@"detail"];
    detail.state = [allTeams objectAtIndex:indexPath.row];
    detail.capital = [states objectForKey:detail.state];
    [self.navigationController pushViewController:detail animated:YES];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSMutableArray *allTeams = [(NSDictionary *)[responseString JSONValue] objectForKey:@"TeamList"];

    NSLog(@"allTeams count, %u",[allTeams count]);
    NSLog(@"Start reload");
        //[self._tableView reloadData];
        NSLog(@"Stop reload");
    }




@end

You're missing some delegate methods. ResponseData is equal to nil. You haven't handled the response data at all. Check out Apple's docs. https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

You're missing the didReceiveData and didReceiveResponse delegate methods. See here:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM