2015-10-06 237 views
0

我已經閱讀了很多很多關於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)

回答

1
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

+0

我很抱歉,但我不明白這是如何相關的UIScrollView行爲。如果您想要UIScrollView行爲使用UIRefreshControl,請使用 – Eddie

+0

。希望這可以幫助你 –

+0

由於缺乏知識,我非常抱歉,但我對'refreshControl'不太瞭解。你能告訴我它是如何作爲'UIScrollView'嗎?我試過將它添加到我的'collectionView'中,但似乎沒有什麼事情發生。如何檢查用戶是否滾動到下一頁或上一頁? – Eddie