滚动时如何添加更多的数据,而不删除数组中的以前的数据?
我有问题试图让用户滚动到底部,如果他们到最后我想通过调用我的服务器加载更多的行。 我正在使用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]; } }
- 在我的设备中安装iPhone应用程序
- iOS objective-C:在浮点上使用模来从“英尺”获得“英寸”
- 如何添加UIViewController作为以编程方式创buildUIView创build的UIButton行动的目标?
- Swift:如何从Objective-C调用一个类或类的方法
- NSDateFormatter这个格式
- 在iOS 7上使用AVSpeechSynthesizer,但保留iOS 6的兼容性
- SignalR .Net客户端失败,设备上出现500个服务器错误,在模拟器上正常工作
- 本地服务器上的ApprtcDemo在浏览器和Android之间工作,但不在iOS上
- Monotouch / Xamarin绑定inheritance