gpt4 book ai didi

iphone - iPhone:请求cellforrow以获取超出范围的行

转载 作者:行者123 更新时间:2023-12-03 17:43:53 26 4
gpt4 key购买 nike

我做错什么了?我刚刚修改了基于导航的应用程序代码,以读取和显示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

最佳答案

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

[indexPath indexAtPosition: [indexPath length] - 1]

改成
[indexPath indexAtPosition: [indexPath length]]

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

关于iphone - iPhone:请求cellforrow以获取超出范围的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/930967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com