【发布时间】:2011-07-07 18:47:45
【问题描述】:
我正在尝试从服务器加载 JSON,然后将其显示在 UITableView 中。请求正常,但是当我尝试将数据添加到 tableview 时,应用程序在我调用时崩溃,然后调用 [tableView reloadData] 方法。我在 UITableView 方法中有变量 jsonArray 引用,以便 TableView 显示该变量的内容。这是最好的方法吗?我做错了什么?
连接
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
主 HTTP 回调
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
//Declare the parser
SBJsonParser *parser = [[SBJsonParser alloc] init];
jsonArray = [parser objectWithString:responseString];
[tableView reloadData];
[parser release];
[responseString release];
}
错误
2011-07-07 14:42:56.923 Viiad[16370:207] -[__NSSet0 objectAtIndex:]: unrecognized selector sent to instance 0x5a4a0b0
2011-07-07 14:42:56.925 Viiad[16370:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 objectAtIndex:]: unrecognized selector sent to instance 0x5a4a0b0'
编辑
UITableViewMethods
// 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];
}
NSArray *results = [[jsonArray valueForKey:@"Rows"] valueForKey:@"name"];
cell.textLabel.text = (NSString *)[results objectAtIndex:indexPath.row];
return cell;
}
【问题讨论】:
-
很可能 jsonArray 不是 NSArray。该应用程序在 tableView:cellForRowAtIndexPath: 方法中失败。可以发一下吗?
标签: iphone objective-c json uitableview