0

テーブル ビューにデータを表示できません。配列には NSLog 出力による有効なデータがあります。の先頭にブレークポイントを設定しましたが、tableView:cellForRowAtIndexPath:そこに到達しません。理由はありますか?

#import "ViewController.h"
#import "Ride.h"

@interface ViewController ()
@property (nonatomic, strong) NSMutableData *responseData;
@end

@implementation ViewController

@synthesize rideIds = _rideIds;
@synthesize rideNames = _rideNames;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"viewdidload");

    self.responseData = [NSMutableData data];


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    // http://www.strava.com/api/v1/segments/229781/efforts?best=true

    // Efforts on segment by athlete limited by startDate and endDate
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.strava.com/api/v1/segments/229781/efforts?athleteId=11673&startDate=2012-02-01&endDate=2012-02-28"]];

    //Leader Board on Segment all Athletes
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.strava.com/api/v1/segments/229781/efforts?best=true"]];

    //Rides by Athlete
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.strava.com/api/v1/rides?athleteId=10273"]];


    //Twitter Example
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/trends"]];


    //Efforts by Ride
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.strava.com/api/v1/rides/77563/efforts"]];

    //Effort Detail
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.strava.com/api/v1/efforts/688432"]];


    //Google API Call
    //NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];

/*    dispatch_async(dispatch_get_main_queue(),^ {
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    } ); */

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(theConnection){
        self.rideIds = [[NSMutableArray alloc]init];
        self.rideNames  = [[NSMutableArray alloc] init];
    } else {
        NSLog(@"No Connection");
        }





}


//Delegate methods for the NSURLConnection

//In order to download the contents of a URL, an application needs to provide a delegate object that, at a minimum, implements the following delegate methods: connection:didReceiveResponse:, connection:didReceiveData:, connection:didFailWithError: and connectionDidFinishLoading:.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSLog(@"didReceiveResponse");

    //This message can be sent due to server redirects, or in rare cases multi-part MIME documents.
    //Each time the delegate receives the connection:didReceiveResponse: message, it should reset any progress indication and discard all previously received data.

    [self.responseData setLength:0];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"didFailWithError");
    NSString *errorDescription = [error description];
//    NSLog([NSString stringWithFormat:@"Connection failed: %@", errorDescription]);
    NSLog(@"Connection failed: %@", errorDescription);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    //NSDictionary *jsonRes = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

    NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
    NSDictionary *jsonRides =[jsonResult objectForKey:@"rides"];


    // Show all values coming out of "rides" key
    // Store ride id's and names on arrays for later display on tableview
    for (NSDictionary *rides in jsonRides) {

        [self.rideIds addObject:[rides objectForKey:@"id"]];
        NSLog(@"id = %@", [rides objectForKey:@"id"]);
        //NSLog(@"%@",self.rideIds);

        [self.rideNames addObject:[rides objectForKey:@"name"]];
        NSLog(@"name = %@", [rides objectForKey:@"name"]);
        //NSLog(@"%@",self.rideNames);
    }
        NSLog(@"%@",self.rideIds);
        NSLog(@"%@",self.rideNames);

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // Show all values coming out of NSKSONSerialization
    for(id key in jsonResult) {

        id value = [jsonResult objectForKey:key];

        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;

        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
    }

    // extract specific value...
    // NSArray *results = [res objectForKey:@"results"];
    /*NSArray *results = [res objectForKey:@"rides"];
    for (NSDictionary *result in results) {
        NSData *athleteData = [result objectForKey:@"name"];
        NSLog(@"Ride name: %@", athleteData);
    }*/


/*    dispatch_async(dispatch_get_main_queue(),^ {
        [self.rideTableView reloadData];
    } ); */

    [self.rideTableView reloadData];


}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"tableView:numberOfRowsInSection: ");

    //return self.rideIds.count;
    NSLog(@"%u",self.rideNames.count);
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"tableView:cellForRowAtIndexPath: ");


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if( nil == cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text= [self.rideNames objectAtIndex:indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tv
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tv deselectRowAtIndexPath:indexPath animated:YES];
}



@end

NSLog コンテンツ:

2012-08-18 18:47:29.497 WebServiceCall[10387:c07] viewdidload
2012-08-18 18:47:29.503 WebServiceCall[10387:c07] tableView:numberOfRowsInSection: 
2012-08-18 18:47:29.503 WebServiceCall[10387:c07] 0
2012-08-18 18:47:29.504 WebServiceCall[10387:c07] tableView:cellForRowAtIndexPath: 
2012-08-18 18:47:29.506 WebServiceCall[10387:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x14b6022 0xeb6cd6 0x14a2d88 0x395d 0xb3c54 0xb43ce 0x9fcbd 0xae6f1 0x57d42 0x14b7e42 0x1d87679 0x1d91579 0x1d164f7 0x1d183f6 0x1da5160 0x17e84 0x18767 0x27183 0x27c38 0x1b634 0x13a0ef5 0x148a195 0x13eeff2 0x13ed8da 0x13ecd84 0x13ecc9b 0x17c65 0x19626 0x22fd 0x2265 0x1)
terminate called throwing an exception(lldb)  

更新:viewDidLoadを通過した後tableview:numberOfRowsInSection、処理のために4つのメソッドすべてをスキップしてメソッドにジャンプするようですNSURLConnection(配列を更新した場所)。

NSURLConnection私のView Controllerは、 my AND myの両方のデリゲートですtableView。最初に tableView メソッドを実行しているようです。最初にメソッドを実行するNSURLConnection方法に関する提案はありますか?

4

2 に答える 2

0

試すことができる2つのこと-まず、numberOfRowsInSectionメソッドにself.rideIds.countを記録して、0が返されないことを確認します。次に、connectionDidFinishLoadingメソッドの最後に、[tableView reloadData](またはテーブルビューは)、接続が完了する前にテーブルビューメソッドが呼び出される問題を処理する必要があります。

編集後:エラー「-[__ NSArrayMobjectAtIndex:]:インデックス0が空の配列の境界を超えています」は、numberOfRowsInSectionメソッドの「return3」が原因で発生しています。アプリが起動すると、接続が結果を返す前にテーブルビューが自動的に入力しようとするため、numberOfRowsInSectionは最初に3ではなく0を返す必要があります。これは、returnself.rideIds.count行を元に戻した場合に行われます。接続デリゲートメソッドの最後でreloadDataを実行すると、配列にデータが入力され、テーブルビューが正しく機能するはずです。

于 2012-08-19T01:17:14.560 に答える
0

はどこtableView:numberOfSectionsInTableView:ですか?設定されていない場合、デフォルトは1ですが、おそらく0を返しています。また、tableView でとを設定する必要があります。delegatedataSource

于 2012-08-18T22:18:14.787 に答える