我想在iOS tableview中实现分页
我需要像iOS tableview中的google一样实现水平分页。我们有没有办法使用一个tableview来实现这一点?您能建议一种方法吗?您从哪里获取数据?如果您有一个API层,那么可以将数据分页构建到API方法中。减少来回传递的实际数据量我想在iOS tableview中实现分页,ios,uitableview,pagination,Ios,Uitableview,Pagination,我需要像iOS tableview中的google一样实现水平分页。我们有没有办法使用一个tableview来实现这一点?您能建议一种方法吗?您从哪里获取数据?如果您有一个API层,那么可以将数据分页构建到API方法中。减少来回传递的实际数据量 您可以通过一个tableView来实现这一点。您需要定义节数以及每个节的项目数。UITableView是UIScrollView的子类。在UITableView委托中执行此操作: - (void)scrollViewDidScroll:(UIScroll
您可以通过一个tableView来实现这一点。您需要定义节数以及每个节的项目数。
UITableView
是UIScrollView
的子类。在UITableView
委托中执行此操作:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 10;
if(y > h + reload_distance) {
NSLog(@"load more rows");
}
}
如果您从web服务获取数据,并且该服务支持分页,则可以实现这一点
如果(y>h+reload\u distance)
为true,则调用从web服务加载更多数据,然后重新加载表视图。希望这有帮助:) 您有两个选项可供选择
1.带分页的滚动视图,并在其上创建UI设计。
2.带转换的Tableview我指的是水平Tableview
UITableView* menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStylePlain];
menuTableView.delegate=self;
menuTableView.dataSource=self;
CGAffineTransform transform = CGAffineTransformMakeRotation(-1.5707963);
menuTableView.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 90, 320, 300);
menuTableView.frame = contentRect;
menuTableView.pagingEnabled= YES;
menuTableView.separatorColor=[UIColor grayColor];
[self.view addSubview:menuTableView];
如果您确定scrollview内存比tableview高,因为您需要在分页时创建多个视图。如果您使用tableview,那么编写代码和处理内存也会很容易
下面是转换tableview的代码
UITableView* menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStylePlain];
menuTableView.delegate=self;
menuTableView.dataSource=self;
CGAffineTransform transform = CGAffineTransformMakeRotation(-1.5707963);
menuTableView.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 90, 320, 300);
menuTableView.frame = contentRect;
menuTableView.pagingEnabled= YES;
menuTableView.separatorColor=[UIColor grayColor];
[self.view addSubview:menuTableView];
希望这对你有帮助。。如果您需要任何其他信息,请告诉我我建议您使用
UICollectionView
而不是UITableView
。也就是说,为了从服务器加载分页数据,您需要添加几行代码:
您需要在视图控制器中添加变量,以便跟踪数据:
/**
* Set this flag when loading data.
*/
@property (nonatomic, assign) BOOL isLoading;
/**
* Set this flag if more data can be loaded.
*/
@property (assign, nonatomic) BOOL hasNextPage;
/**
* The current page loaded from the server. Used for pagination.
*/
@property (assign, nonatomic) int currentPage;
根据您使用的实现,有不同的实现方法,我们将在其中检查是否是加载数据的正确时间 UITableView 并在
UITableViewDelegate
实现中添加:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = tableView.contentOffset.y;
CGFloat height = tableView.contentSize.height - tableView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = collectionView.contentOffset.y;
CGFloat height = collectionView.contentSize.height - collectionView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
UICollectionView
并在UICollectionViewDelegate
实现中添加:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = tableView.contentOffset.y;
CGFloat height = tableView.contentSize.height - tableView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
// Check scrolled percentage
//
CGFloat yOffset = collectionView.contentOffset.y;
CGFloat height = collectionView.contentSize.height - collectionView.height;
CGFloat scrolledPercentage = yOffset / height;
// Check if all the conditions are met to allow loading the next page
//
if (scrolledPercentage > .6f && !self.isLoading && self.hasNextPage)
[self loadNextPage:++self.currentPage];
}
然后加载下一页:
- (void)loadNextPage:(int)pageNumber {
if (self.isLoading) return;
self.isLoading = YES;
// Fetch your data here
// ..
// Once the request is finished, call this
self.isLoading = NO;
}
我还建议您检查滚动方向,并将其添加为加载下一页的条件。这说明了如何执行此操作。使用
UICollectionView
?是否需要使用垂直滚动分页also@CharanGiri我只需要水平滚动,这样每个滚动将重新加载来自后端的不同数据。您应该在问题中添加更多详细信息,您要查找的内容不清楚。@jbouaziz最初的tableview有10条记录。然而,有100条记录来自后端。我们可以在这个tableview中进行垂直分页,但是是否有任何方法可以滑动tableview,以便在同一个tableview中显示新数据。我使用SMP2.3中的MBO获取数据。传入的数据包含100条记录,我需要一次显示20条记录。我是iOS新手。我们可以使用节数进行水平滚动吗?我想他指的是从服务器对数据进行分页,而不是滚动视图。