ios - UICollectionViewCell 变为隐藏=YES

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewdelegate

我正在尝试使用 UICollectionView 实现“无限滚动”。

我通过像 this tutorial 中那样缓冲我的数据数组来做到这一点

然后通过以下方式实现 UICollectionViewDelegatedidEndDisplayingCell:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.galleryArray.count > 0) {

    NSIndexPath *newIndexPath = indexPath;

    if (self.specialHeaderView.bannerView.scrollDirection == left) {
        newIndexPath = [NSIndexPath indexPathForItem:indexPath.row - 1 inSection:indexPath.section];
    } else if (self.specialHeaderView.bannerView.scrollDirection == right) {
        newIndexPath = [NSIndexPath indexPathForItem:indexPath.row + 1 inSection:indexPath.section];
    }

    if (newIndexPath.row == (self.galleryArray.count - 1)) {
        // user is scrolling to the right from the last item to the 'fake' item 1.
        // reposition offset to show the 'real' item 1 at the left-hand end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:1 inSection:indexPath.section];

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return;

    }

    //        if (scrollView.contentOffset.x == self.collectionView.frame.size.width)  {
    if (newIndexPath.row == 0) {
        // user is scrolling to the left from the first item to the fake 'item N'.
        // reposition offset to show the 'real' item N at the right end end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:([self.galleryArray count] -2) inSection:indexPath.section];

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    }


}

问题是,每当调用 didEndDisplayingCell 方法并且 Collection View 通过它的委托(delegate) CellForItemAtIndexPath 方法请求一个单元格时,该单元格就会隐藏起来。

这是我的CellForItemAtIndexPath 实现:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SpecialBannerCell *specialBannerCell = (SpecialBannerCell *)[collectionView dequeueReusableCellWithReuseIdentifier:GalleryCellIdentifier forIndexPath:indexPath];
if (specialBannerCell.hidden) {

}
Benefit *benefit = [self.galleryArray objectAtIndex:indexPath.row];
[specialBannerCell.imageBanner setImageWithURL:[NSURL URLWithString:benefit.imageIphoneUrl] placeholderImage:[UIImage imageNamed:@"photo_loader"]];
return specialBannerCell;

我在这里做错了什么?

最佳答案

所以我不确定为什么,但是当我使用 UIScrollViewDelegate 方法而不是 didEndDisplayingCell 方法时,单元格不再隐藏并且一切正常。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (self.galleryArray.count > 0) {

    NSIndexPath *indexPath = self.currentIndexPath;

    NSIndexPath *newIndexPath = indexPath;

    if (newIndexPath.row == (self.galleryArray.count - 1)) {
        // user is scrolling to the right from the last item to the 'fake' item 1.
        // reposition offset to show the 'real' item 1 at the left-hand end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:1 inSection:indexPath.section];

        self.currentIndexPath = newIndexPath;

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return;

    }

    if (newIndexPath.row == 0) {
        // user is scrolling to the left from the first item to the fake 'item N'.
        // reposition offset to show the 'real' item N at the right end end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:([self.galleryArray count] -2) inSection:indexPath.section];

        self.currentIndexPath = newIndexPath;

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    }


}

关于ios - UICollectionViewCell 变为隐藏=YES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352030/

相关文章:

ios - 核心图:2Y 图缩放问题

javascript - CSS 和 Javascript 未在 iOS webview 中加载

ios - 为什么我的代码只将 TIMEPROFILESUBSEGS 的第一个元素插入到数据库中?

objective-c - 如果标签包含 NSAttributed 字符串且部分字符串为粗体,如何获取标签的准确高度

ios - 在静态 TableView 单元格中实现照片库

ios - 为什么我的 uipagecontrol 第一次没有正确更新,但是第二次通过 Xcode 更新了

ios - 同时运行 Collection View 和 TableView 时应用程序崩溃。崩溃 :fatal error: unexpectedly found nil while unwrapping an Optional value

iOS:导入笔记的API

iphone - iOS 中缓存的 NSURLRequest 内存大小

ios - 如何检测滚动到 UICollectionView 中的新部分?