【发布时间】:2014-06-22 14:05:11
【问题描述】:
我正在尝试使用UICollectionView 实现“无限滚动”。
我通过像this tutorial 那样缓冲我的数据数组来做到这一点
然后通过以下方式实现UICollectionViewDelegate的didEndDisplayingCell:
- (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;
}
我在这里做错了什么?
【问题讨论】:
-
你的单元格是隐藏在你的界面生成器还是你的xib文件中?
-
@LenaBru 一点也不。正如我所提到的,它确实在大多数情况下出现。无论如何,我解决了这个问题 - 解决方案发布在下面。
-
我在 cellforrowat 索引路径中也遇到了类似的问题,我在 Pageview 控制器中有集合视图横幅,第一次我在页面上滑动时,一切都很好,但是当我向后滑动单元格时隐藏属性为 YES。相同的代码在 iOS8 中运行良好,在 iOS9 中停止运行
标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewdelegate