我需要在 iOS 的 tableview 中实现类似 Google 的水平翻页效果。有没有办法使用单个 tableview 实现这个功能?您能提供一种方法来实现吗?
我需要在 iOS 的 tableview 中实现类似 Google 的水平翻页效果。有没有办法使用单个 tableview 实现这个功能?您能提供一种方法来实现吗?
我建议您使用 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;
根据你使用的实现方式,有不同的方法来检查是否是合适的时间来加载数据。
在你的 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];
}
在你的 UICollectionViewDelegate
实现中添加:
- (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;
}
我建议您检查滚动方向,并将其作为加载下一页的条件之一。这个答案解释了如何执行该操作。
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");
}
}
if(y > h + reload_distance)
为真,则调用Web服务以加载更多数据,然后重新加载表视图即可。希望这有所帮助.. :)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
? - nhgrif