2012-03-18 47 views
0

我是新來解析目標C中的json文件,我想從列表中解析Twitter json訂閱用戶名。 我想獲取用戶的關注者列表,並解析他們以獲取user_ids,然後再次調用另一個URL來獲取他們的姓名和個人資料圖片。 我得到的人的名字,但我無法正確解析列表。 如果有人能幫助我,這將會非常有幫助。 我對獲取數據代碼:添加數組到單元行來自Twitter客戶端訂閱iphone

-(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]; – 2012-03-18 23:08:19

回答

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在這裏所做的工作沒有必要通過數組循環,在tableview中會調用每個對象的數組每個單元英寸

+0

作爲一個方面說明,我不會在每個對象被加載到循環中的數組之後重新加載tableview,只需在數組填充所有名稱後執行一次。 – 2012-03-18 23:20:11

+0

感謝Hubert!但如上所述更改爲代碼不會在每個單元格上顯示我的列表。它變空了。 – Change 2012-03-18 23:25:03

+0

用戶名數組填入所有名稱?你是否實施了其他代表? numberOfRows和numberOfSections? – 2012-03-18 23:29:41

相關問題