Ios 在UITableView的底部加载更多UICell
我启动了一个iOS项目,我正在使用UITableView显示带有图像的飞行员列表。我对我的api进行了分页,并尝试在您滚动tableview后加载更多内容。我遇到的问题是,新单元格总是显示在tableview的顶部,而不是底部。请检查我的代码,如果有解决方案,我将不胜感激Ios 在UITableView的底部加载更多UICell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我启动了一个iOS项目,我正在使用UITableView显示带有图像的飞行员列表。我对我的api进行了分页,并尝试在您滚动tableview后加载更多内容。我遇到的问题是,新单元格总是显示在tableview的顶部,而不是底部。请检查我的代码,如果有解决方案,我将不胜感激 - (void)loadData :(NSInteger)page { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.
- (void)loadData :(NSInteger)page {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@%ld",NSLocalizedString(@"get_pilots",nil),mainDelegate.idAccount,@"?page=",(long)page]];
task = [restObject GET:url :mainDelegate.token completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary* jsonResponse = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:nil];
NSArray *pilotKey = [jsonResponse objectForKey:@"pilot"];
for (NSDictionary *pilotItem in pilotKey ){
PilotObject *pilotObj = [PilotObject new];
[pilotObj getPilot:pilotObj :pilotItem];
[_pilotsAll addObject:pilotObj];
}
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
[self checkTableView:_pilotsDisplay :self.view];
[viewPilots.tableViewPilots reloadData];
});
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (currentPage == totalPages) {
return [_pilotsDisplay count];
}
return [_pilotsDisplay count] + 1;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [_pilotsDisplay count] - 1 && currentPage<totalPages ) {
[self loadData:++currentPage];
NSLog(@"current page : = %ld",(long)currentPage);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [_pilotsDisplay count]) {
static NSString *identifier = @"PilotCellTableViewCell";
PilotCellTableViewCell *cell = (PilotCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
cell.hidden=YES;
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[cell.contentView viewWithTag:100];
[activityIndicator startAnimating];
return cell;
} else {
PilotObject *pilotObjDisplay = nil;
pilotObjDisplay = [_pilotsDisplay objectAtIndex:[_pilotsDisplay count]-1-indexPath.row];
static NSString *identifier = @"PilotCellTableViewCell";
PilotCellTableViewCell *cell = (PilotCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
cell.hidden=NO;
cell.image.image = pilotObjDisplay.imageDisplayPilot;
cell.titleLabel.text = pilotObjDisplay.firstName;
cell.subTitleLabel.text = pilotObjDisplay.lastName;
cell.backgroundColor = [UIColor colorWithHexString:NSLocalizedString(@"gray_background", nil)];
return cell;
}
return nil;
}
-(无效)加载数据:(NSInteger)页面{
MBProgressHUD*hud=[MBProgressHUD ShowHUD AddedTo:self.navigationController.view动画:是];
url=[NSURL URLWithString:[NSString stringWithFormat:@“%@%@%@%ld”,NSLocalizedString(@“get_pilots”,nil),mainDelegate.idAccount,@“?”,(长)页];
task=[restObject GET:url:mainDelegate.token completionHandler:^(NSData*数据,NSURLRResponse*响应,NSError*错误){
NSMutableDictionary*jsonResponse=[NSJSONSerialization
JSONObjectWithData:数据
选项:针织品
错误:无];
NSArray*pilotKey=[jsonResponse objectForKey:@“pilot”];
用于(NSDictionary*pilotKey中的pilotItem){
PilotObject*pilotObj=[PilotObject new];
[pilotObj getPilot:pilotObj:pilotItem];
[_pilotsalladdobject:pilotObj];
}
dispatch\u async(dispatch\u get\u main\u queue()^{
[抬头显示器隐藏:是];
[self-checkTableView:_-pilotsDisplay:self.view];
[viewPilots.tableViewPilots重新加载数据];
});
}];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
如果(currentPage==totalPages){
返回[_PilotsDisplayCount];
}
返回[_PILOTSDISPLAYCOUNT]+1;
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(NSIndexPath*)indexPath{
如果(indexPath.row==[\u-pilotsDisplay count]-1&¤tPage为什么选择2个数组\u-pilotsDisplay和\u-pilotsAll
如果没有必要,还可以使用一个NSMutableArray
进行分页,在从服务器获取数据以及将数据填充到UITableView
时,都可以使用该数组
记住一件事,只需在viewDidLoad
方法中初始化您的NSMutableArray
。当您收到新数据时,使用您已经使用的NSMutableArray
方法。然后调用UITableView
的reloadData
方法
在cellforrowatinexpath
中,不要使用像[[u pilotsDisplay count]-1-indexPath.row
这样的计算,只需使用indexPath.row
在这里,将行插入到tableview可能会对您有所帮助
[tableView beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
您不应该将单元格添加到tableview。您应该做的是将数据添加到tableview的数据源(在您的示例中为_pilotsDisplay),然后简单地重新加载表。如果您希望新数据显示在底部或以任何特定顺序出现,您应该对数据源(数组)执行此操作.检查您的回答数据是如何产生的?因为tableview只表示您提供给它的数据。数据很好,它带有分页功能,在android上工作。我的问题是如何将新数据放在tableview的底部,而不是iOS上的顶部