4

我正在尝试使用UICollectionView.

我通过像本教程中那样缓冲我的数据数组来做到这一点

然后通过以下方式实现didEndDisplayingCellof :UICollectionViewDelegate

- (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调用该方法并且集合视图通过其委托方法请求一个单元格时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;

}

我在这里做错了什么?

4

1 回答 1

0

所以我不知道为什么,但是当我使用 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];

    }


}

}

于 2014-06-23T06:07:01.723 回答