滚动时如何添加更多的数据,而不删除数组中的以前的数据?

我有问题试图让用户滚动到底部,如果他们到最后我想通过调用我的服务器加载更多的行。 我正在使用AFNetworking的呼叫,并在完整的callback我重新加载的数据,但是,当它调用更多的数据擦除之前,我想它添加更多。

这是我现在的代码:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row + 50 > [self.userIds count]) //self.array is the array of items you are displaying { [self getMoreFollowing]; //If it is the last cell, Add items to your array here & update the table view } } -(void)getMoreFollowing { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // NSDictionary *parameters = @{@"userId": userProfileId, @"count": @(count)}; manager.requestSerializer = [AFJSONRequestSerializer serializer]; //manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format [manager GET:@"http://example.com/users" parameters:@{ @"userId": userProfileId, @"count": @(count)} success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); NSLog(@"Data saved"); //set other arrays for passing data [userIds addObjectsFromArray: [responseObject valueForKeyPath:@"_id"]]; [names addObjectsFromArray: [responseObject valueForKeyPath:@"name"]]; count = [userIds count]; [self.tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; } 

我曾尝试添加它

 @property (strong, nonatomic) NSMutableArray *userIds; 

更新:没有错误,但表甚至没有创build,它保持空白,如果我设置userIds =以及其他人,那么它覆盖与调用…什么需要发生?

创build你的数组的可变副本和插入对象,而不是添加然后设置你的数组。

 [mutableCopyOfYourArray insertObject:(id) atIndex:(NSUInteger)]; yourArray = mutableCopyOfYourArray; 

在索引处插入对象会将原先占据索引的对象移动1,并在那里插入你想要的东西。 我怀疑这是你想做什么。 您只需使用addObject在数组的末尾插入项目并重新加载表数据

 [mutableCopyofYourArray addObject:yourNewitem]; yourArray=mutableCopyofYourArray; [self.tableView reloadData]; 

如果要在顶部添加项目,则使用insertObject atIndex并将索引设置为0

尝试这个

 -(void)getMoreFollowing { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // NSDictionary *parameters = @{@"userId": userProfileId, @"count": @(count)}; manager.requestSerializer = [AFJSONRequestSerializer serializer]; //manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format [manager GET:@"http://example.com/users" parameters:@{ @"userId": userProfileId, @"count": @(count)} success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); NSLog(@"Data saved"); //set other arrays for passing data [userIds addObjectsFromArray: [responseObject valueForKeyPath:@"_id"]]; [names addObjectsFromArray: [responseObject valueForKeyPath:@"name"]]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; count = [userIds count]; [self.tableView reloadData]; 

}

我最终创build了第二套NSMutable Arrays,并且像这样连接了两个:

 [userIdsArray addObjectsFromArray:userIds]; 

然后这样调用它:

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row + 50 > [userIdsArray count]) //self.array is the array of items you are displaying { [self getFollowers]; } }