UITableView и асинхронная загрузка с NSURLconnection - PullRequest
0 голосов
/ 02 марта 2012

У меня возникла проблема, которая для меня выглядит так, как будто мой JSON URL-запрос завершается после загрузки моего UITableView. Я попытался перезагрузить 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 ]

1 голос
/ 02 марта 2012

Вам не хватает методов делегатов didReceiveData и didReceiveResponse.Смотрите здесь:

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

1 голос
/ 02 марта 2012

Вам не хватает некоторых методов делегатов. ResponseData равен нулю. Вы не обработали данные ответа вообще. Проверьте документы Apple. https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

...