2012-03-18 16 views
0

私は目的CのjSONファイルを解析するのが初めてで、Twitterのjsonを解析してリストからユーザー名を取得したいと思っています。 私はユーザーのフォロワーのリストを取得し、それらを解析してuser_idsを取得し、再度別のURLを呼び出して名前とプロフィール写真を取得しようとしています。 人の名前を取得していますが、リストを正しく解析できません。 誰かが私をここで助けることができれば、本当に役に立ちます。 データをフェッチするための私のコード:iphoneのTwitterクライアントフィードから配列をCell Rowに追加する

-(void) fetchData{ 


ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error){ 

    if (granted == YES) { 

     NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType]; 

     if ([arrayOfAccounts count] > 0) { 
      ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; 
      NSString *username = acct.username; 


      NSLog(@"Account : %@", username); 

      TWRequest *fetchFriendsFollowers = [[TWRequest alloc] 
               initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/followers/ids.json?screen_name=%@",acct.username]] 
               parameters:nil 
               requestMethod:TWRequestMethodGET]; 
      [fetchFriendsFollowers performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){ 

       if ([urlResponse statusCode] == 200) { 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         NSError *jsonParsingError = nil; 
         NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; 
         self.responseArray = [response objectForKey:@"ids"]; 

         for (int i =0 ; i < [self.responseArray count]; i++) { 
          NSString *user_id = [self.responseArray objectAtIndex:i]; 


          TWRequest *fetchFriendsFollowersNames = [[TWRequest alloc] 
                    initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/lookup.json?user_id=%@",user_id]] 
                    parameters:nil 
                    requestMethod:TWRequestMethodPOST]; 

          [fetchFriendsFollowersNames performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){ 
           if ([urlResponse statusCode] == 200) { 
            dispatch_async(dispatch_get_main_queue(), ^{ 
             NSError *jsonParsingError = nil; 
             NSArray *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; 
             for (NSDictionary *user in response) { 

              [self.userNameArray addObject:[user objectForKey:@"name"]]; 

              [self.tableView reloadData]; 
             } 



            }); 
           } 



          }]; 

         } 

         NSLog(@"responseArray %@ for user: %@",self.responseArray,username); 
        }); 
       } 

      }]; 

     } 
     else{ 

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"Please add twitter accounts on your phone and log back in." 
                  delegate:nil 
                cancelButtonTitle:@"Ok" 

                otherButtonTitles: nil]; 
      [alert show]; 
     } 

    } 
}]; 

}

そして私はcellForRowAtIndexPathですべてのユーザー名のリストを、それを表示しています。実際にはリストを取得し、すべてのセルでその名前を繰り返します。私は何か愚かな間違いをしていることを知っているが、私はこれをしばらく見ていて、それを修正することができないので、理解できない。

- (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]; 
} 

for (int i=0; i < [self.userNameArray count]; i++) { 

    NSLog(@"Text : %@", [self.userNameArray objectAtIndex:i]); 
    cell.textLabel.text = [self.userNameArray objectAtIndex:i]; 

} 
return cell; 

}

+0

forループだけは必要ありません。cell.textLabel.text = [self.userNameArray objectAtIndex:indexPath.row]; –

答えて

0
- (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]; 
     } 




      cell.textLabel.text = [self.userNameArray objectAtIndex:indexPath.row]; 
      NSLog(@"Text : %@", [self.userNameArray objectAtIndex:indexPath.row]); 

     return cell; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
    return [self.userNameArray count]; 
    } 

indexPath.rowはここで働いてアレイをループの必要はありませんを行い、テーブルビューは、各セルの配列内の各オブジェクトを呼び出します。

+0

副作用として、各オブジェクトがループ内の配列にロードされた後では、テーブルビューをリロードすることはありません。配列にすべての名前が設定された後に一度だけ行います。 –

+0

ありがとうHubert!上記のようにコードを変更しても、各セルにリストは表示されません。それは空になる。 – Change

+0

そして、userNameArrayにはすべての名前が設定されていますか?他の代議員を実装しましたか? numberOfRowsとnumberOfSections? –

関連する問題