将数组从iphone中的Twitter客户端提要添加到Cell Row中



我是解析目标C中的jSON文件的新手,我想解析列表中的Twitterjson提要用户名称。我正在尝试获取用户的关注者列表,并对其进行解析以获取user_id,然后再次调用另一个URL以获取他们的姓名和个人资料图片。我正在得到这些人的名字,但我无法正确解析列表。如果有人能在这里帮助我,那将是非常有帮助的。我获取数据的代码:

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

}

    - (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将为每个单元格调用数组中的每个对象。

最新更新