iPhone: запрос на мобильный для строк за пределами - PullRequest
0 голосов
/ 31 мая 2009

Что я сделал не так? Я просто немного изменил код приложения на основе навигации, чтобы прочитать и отобразить строку JSON. Он падает, когда я прокручиваю список с сообщением Objc_msgSend и указывает на это как на проблему: cell.textLabel.text = [[location objectAtIndex: storyIndex] objectForKey: @ "title"];

 #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

Ответы [ 2 ]

0 голосов
/ 01 июня 2009

решение:

self.locations = [jsonstring JSONValue];

Спасибо за все предложения.

0 голосов
/ 31 мая 2009

0 основанные массивы, длина id равна 0, 0 - 1 = -1, это, очевидно, вызовет исключение.

[indexPath indexAtPosition: [indexPath length] - 1]

изменить на

[indexPath indexAtPosition: [indexPath length]]

и вы уверены, что не хотите indexPath.row. похоже, вы просто возвращаете длину каждый раз.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...