使用NSURLconnection加载UITableView和Async

时间:2012-03-02 03:13:09

标签: iphone ios ios4 nsurlconnection

我遇到了一个问题,在我加载UITableView后,我的JSON URL请求似乎已经完成了。我试图重新加载UITableview,但是要么工作不正常,要么没有像其他一些问题所说的那样做。这个viewcontroller正在关闭AppDelegate。如果有人看到我做得不对的话,我会很感激。

#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

2 个答案:

答案 0 :(得分:1)

你错过了一些委托方法。 ResponseData等于nil。您根本没有处理响应数据。查看Apple的文档。 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

答案 1 :(得分:1)