ios - 使用分页显示 UICollectionView 的正确方法

标签 ios objective-c uiscrollview uicollectionview paging

我已经阅读了很多关于带分页的 UICollectionView 的 SO 解决方案。我自己也使用这些解决方案:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.collectionView.frame.size.width;
    self.lbPaging.text = [NSString stringWithFormat:@"%d/%d", self.collectionView.contentOffset.x / pageWidth, totalPage];
}

- (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)。

Precisely for us developer or tester, to reproduce this bug, keep your finger touch on the scroll view and start to scroll. When you're nearly out of the edge (where normally, you'll release your finger), touch the other side of the edge and keep scrolling. It's easier to reproduce on real device.

所以我问是否有人知道显示页面导航的正确方法?好吧,没有 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。

希望这对你有帮助...

请引用我的图片

enter image description here

关于ios - 使用分页显示 UICollectionView 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32966611/

相关文章:

iphone - performSelectorOnMainThread 嵌入在 didUpdateToLocation

objective-c - 如何将受密码保护的 PDF 保存为未受密码保护的 PDF

ios - 将 ScrollView 添加到我的 viewController

ios - 具有许多图像的 UIScrollView 性能不佳

android - 如何使用 Cordova 文件插件读取/写入 Android/iOS 设备子目录中的文件?

ios - 与后面的 View Controller 交互

ios - 无法快速获取要在 UITextView 中显示的正确文件

ios - 重新加载数据后 Tableview 偏移量发生变化

ios - 如何在 Objective-C 中返回类似于 std::pair 的对象?

ios - UIScrollView 中图像上的 cropImage 方法问题产生错误的图像