2011-07-07 6 views
1

severからJSONをロードして、それをUITableViewに表示しようとしています。要求はうまく行くが、私は[tableView reloadData]メソッドが呼び出されたときにアプリケーションがクラッシュするテーブルビューにデータを追加しようとすると呼び出されます。 UITableViewメソッドで変数jsonArrayの参照があるので、TableViewはその変数の内容を表示します。これを行うにはこれが最善の方法ですか、何が間違っていますか?iITableViewでJSONを非同期的にiPhoneにロードする

接続

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のインスタンスに送信されたことを意味します。私には、あなたはNSArrayを期待しているNSSetを回っているようです。

[results objectAtIndex:indexPath.row]を呼び出す前にデバッガのブレークポイントを設定して、実際に何が入っているのかを確認してください。results私の推測では、JSONパーサはあなたが思うものを返さないということです。 Objective-Cは、変数がNSArray型であると言うだけで、他のオブジェクトを含めることができないというわけではありません。関数がタイプidを返す場合、コンパイラがチェックするための型情報はまったくありません。

関連する問題