2009-05-31 5 views
0

Qu'est-ce que j'ai fait de mal? Je viens de modifier un peu le code de l'application Navigation Based pour lire et afficher une chaîne JSON. Il se bloque lorsque je fais défiler la liste avec le message Objc_msgSend et pointe le problème comme suit: cell.textLabel.text = [[emplacements objectAtIndex: storyIndex] objectForKey: @ "title"];iPhone: demande de cellule pour des lignes en dehors des limites

#import "RootViewController.h" 
#import "JSON.h" 


@implementation RootViewController 

@synthesize locations; 

- (NSString *)stringWithUrl:(NSURL *)url 
{ 
    // Construct a String around the Data from the response 
    return [[NSString alloc] initWithContentsOfURL:url]; 
} 

-(void)jsonLoad { 

    NSURL *URL = [NSURL URLWithString:@"http://bombaytokyo.com/whrru/jsonexample.html"]; 

    NSString *jsonstring = [self stringWithUrl:URL]; 
    NSLog(jsonstring); 
    locations = [jsonstring JSONValue]; 
    [jsonstring release]; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    [self jsonLoad]; 

} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
    [locations release]; 
} 

- (void)viewDidUnload { 
    // Release anything that can be recreated in viewDidLoad or on demand. 
    // e.g. self.myOutlet = nil; 
} 


#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// return 0; 
    return [locations count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    NSLog(@"Index Path: %i",indexPath); 
    //NSLog(title); 
    cell.textLabel.text=[[locations objectAtIndex: storyIndex] objectForKey: @"title"]; 

    return cell; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

Répondre

0

0 tableaux à base, longueur id est 0 0 - 1 = -1 cela va évidemment provoquer une exception.

[indexPath indexAtPosition: [indexPath length] - 1] 

changement

[indexPath indexAtPosition: [indexPath length]] 

et vous êtes sûr que vous ne voulez pas indexPath.row. On dirait que vous revenez juste à chaque fois.

0
solution

:

self.locations = [jsonstring JsonValue];

Merci pour toutes les suggestions.

Questions connexes