1

我想用UICollectionView.

//detect the bottom and add new data
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.bounds.size.height)) {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
        NSLog(@"%s",__PRETTY_FUNCTION__);
    //ensure that the end of scroll is fired.
    [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.3];
        }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    currentPage+=1;
    // First figure out how many sections there are
    NSInteger lastSectionIndex = [self.collectionView numberOfSections] - 1;

    NSInteger lastItemIndex = [self.collectionView numberOfItemsInSection:lastSectionIndex] - 1;

    NSIndexPath *pathToLastItem = [NSIndexPath indexPathForItem:lastItemIndex inSection:lastSectionIndex];

    [Flickr searchFlickrForTerm:_searchBar.text page:currentPage completionBlock:^(NSString *searchTerm, NSArray *results, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.searchResults[searchTerm] addObjectsFromArray:results];
            [self.collectionView reloadData];
            [_collectionView scrollToItemAtIndexPath:pathToLastItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
        });
    }];

}

问题是scrollViewDidEndScrollingAnimation每次scrollToItemAtIndexPath:执行方法时都会触发。结果,我看到了一个连续的幻灯片。

我尝试使用dispatch_after不同的延迟,但没有帮助。

contentSize据我了解, scrollView不会更新它[self.collectionView reloadData];

如何解决?

4

1 回答 1

3

只需使用内置无限滚动的 SVPullToRefresh 即可。由于 SVPullToRefresh 是建立在 . 之上的UIScrollView,因此它适用于任何其他也建立在UIScrollView类似UITableViewor之上的东西UICollectionView

https://github.com/samvermette/SVPullToRefresh/

于 2013-09-02T15:56:09.680 回答