用分页显示UICollectionView的正确方法
问题描述:
我已经阅读了很多很多关于UICollectionView分页的解决方案。我自己也有那些解决方案的工作:用分页显示UICollectionView的正确方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.collectionView.frame.size.width;
self.lbPaging.text = [NSString stringWithFormat:@"%d/%d", self.collectionView.contentOffset.x/pageWidth, totalPage];
}
OR
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.collectionView.frame.size.width;
self.lbPaging.text = [NSString stringWithFormat:@"%d/%d", self.collectionView.contentOffset.x/pageWidth, totalPage];
}
呀,这做工精细大部分的时间。但是现在我遇到了一些意想不到的事情:
当用户滚动得足够快时,它会超过上面的方法,这可能会导致分页故障(如15/13在最后一页,或显示3/5,即使它是第一页)。
准确地说,对于我们的开发人员或测试人员来说,要重现此错误,请保持手指触摸滚动视图并开始滚动。当你几乎走出边缘(通常,你会松开你的手指),触摸边缘的另一边,并保持滚动。在真实设备上复制更容易。
于是我问,如果有人知道,以显示页面导航有道?那么,没有UIPageViewController(我想显示数字页面,而不是点)。
编辑
我不知道这个问题是严重的或没有,但我觉得可能是因为我滚动到下一个页面执行时加载数据。最近,我有崩溃在这(这真的很难调试,就像步它得到了坠毁在其中):
[self.collectionView performBatchUpdates:^{
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (NSInteger index = 0; index < next2page.count; index++) {
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:(index + offset) inSection:0]];
}
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
} completion:nil];
例外是这样的:失败插入在第0项指数路径(有只有69行中,插入到indexPath:73)
答
hello i have create button in UICollectionView like load more data and function is given below:-
- (void)viewDidLoad {
//declare page inedex initial base 0.
self.pageIndex = 0;
[self loadMoreData];
}
之后您调用加载更多的数据功能页面指数将增加在这里我要像0,20,40我的网页索引,所以我必须创建self.pageIndex + = 20单位可根据您的需要更改
-(void)loadMoreData
{
self.isFromMoreData = TRUE;
self.pageIndex+= 20;
NSLog(@"loadMoreData %d and self.index %d",self.pageIndex,self.index);
[self loadDataIntoCollectionView];//this function calls api
}
******************另一种方式如下************************* ******
使用UIRefreshControl。
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl = refreshControl;
refreshControl.backgroundColor = [UIColor whiteColor];
refreshControl.tintColor = [UIColor grayColor];
[refreshControl addTarget:self action:@selector(loadMoreData) forControlEvents:UIControlEventValueChanged];
self.myCollectionView.pagingEnabled = YES;
[self.myCollectionView addSubview:refreshControl];
self.myCollectionView.alwaysBounceVertical = YES;
并调用上面声明的相同函数loadMoreData。
希望这有助于你...
请提及我的形象
我很抱歉,但我不明白这是如何相关的UIScrollView行为。如果您想要UIScrollView行为使用UIRefreshControl,请使用 – Eddie
。希望这可以帮助你 –
由于缺乏知识,我非常抱歉,但我对'refreshControl'不太了解。你能告诉我它是如何作为'UIScrollView'吗?我试过将它添加到我的'collectionView'中,但似乎没有什么事情发生。如何检查用户是否滚动到下一页或上一页? – Eddie