2011-07-07 120 views
1

我想从服务器加载JSON,然后在UITableView中显示它。请求很好,但是当我尝试将数据添加到tableview时,应用程序崩溃时,我打电话[tableView reloadData]方法被调用。我在UITableView方法中有变量jsonArray引用,以便TableView显示该变量的内容。这是做这件事的最好方式,我做错了什么?在iPhone上UITableView异步加载JSON

连接

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' 

EDIT

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; 
} 
+0

jsonArray不是NSArray的机会很好。该应用在tableView:cellForRowAtIndexPath:方法中失败。你可以发布吗? – ageektrapped

+0

好吧,我刚刚添加它。谢谢! –

回答

2

此消息

-[__NSSet0 objectAtIndex:]: unrecognized selector sent to instance 0x5a4a0b0 

意味着在代码某处时,objectAtIndex:消息被发送到的__NSSet0一个实例。对我来说,看起来你正在传递一个NSSet,你期望NSArray

在调用[results objectAtIndex:indexPath.row]之前设置调试器断点并查看results实际包含的内容。我的猜测是JSON解析器没有返回你认为的结果。 Objective-C是动态类型的:仅仅因为你说变量是NSArray类型并不意味着它不能包含其他对象。如果函数返回类型id,编译器根本没有任何类型信息。