2012-03-02 82 views
0

我有一个问题,我看起来像我的JSON URL请求完成后,我加载我的UITableView。我试图重新加载UITableview,但是无法正常工作,或者没有像其他一些问题所建议的那样做。这个viewcontroller正在从AppDelegate下来。如果有人看到我不擅长的事情,会很感激。UITableView和异步加载与NSURLconnection

#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 

回答