iPhone:请求行超出界限的cellforrow

时间:2009-05-31 00:46:21

标签: iphone uitableview crash

我做错了什么?我刚刚修改了基于导航的应用程序代码以读取和显示JSON字符串。当我使用Objc_msgSend消息向上滚动列表并将其指向问题时崩溃:cell.textLabel.text = [[locations 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 :(得分:0)

0基于数组,id长度为0 0 - 1 = -1这显然会导致异常。

[indexPath indexAtPosition: [indexPath length] - 1]

更改为

[indexPath indexAtPosition: [indexPath length]]

你确定你不想要indexPath.row。看起来你每次只是返回长度。

答案 1 :(得分:0)

解决方案:

self.locations = [jsonstring JSONValue];

感谢所有建议。