UICollectionView无限滚动?

我试图实现无限滚动行为的集合视图。 滚动应该是圆形的 – 就像在UIPickerView 。 可以用苹果的UICollectionView来完成,还是别无select,只能创build一个水平的PickerView?

你怎么看?

您应该观看WWDC 2011会话video“高级滚动视图技术”,他们谈论无限滚动,我敢肯定有可能使用UICollectionView s。 当您滚动时,它将重新调入,并将新项目放在当前可见项目之后(或之前,取决于滚动方向)。

就像他们在会议中所说的那样,你不应该使用一个有结局的方法。 就像在会议中所说:人们一定会find边缘。

是的,你可以做到这一点。 为collectionView:numberOfItemsInSection:返回一个非常大的数字,在collectionView:cellForItemAtIndexPath中返回一个非常大的数字:使用mod操作符总是返回一个indexPath.Row的数字,该数字介于0和数据数组中的最后一个索引之间。 所以如果你的数组中有20个项目,你可以这样做:

 item.whatever.text = [self.theData objectAtIndex:indexPath.row %20]; 

重写此方法以启用无限滚动两种方式。 只要距离中心太远,用户永远不会到达目的地,就将内容放在中心位置。

 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint currentOffset = self.collectionView.contentOffset; CGFloat contentWidth = self.collectionView.contentSize.width; CGFloat centerOffsetX = (contentWidth - self.collectionView.bounds.size.width) / 2.0; CGFloat distanceFromCenter = fabs(currentOffset.x - centerOffsetX); if (distanceFromCenter > (contentWidth / 4.0)) { self.collectionView.contentOffset = CGPointMake(centerOffsetX, currentOffset.y); } } 

感谢rdelmar一个棘手的解决scheme。 “对于collectionView:numberOfItemsInSection大量使用”的想法就像一个魅力。 我试图用UIScrollView委托方法实现相同。 但是输出是生涩的。

这是我的代码的一部分。

 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 100;} 

// CellforRow

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // Setup cell identifier static NSString *cellIdentifier = @"cvCell"; CVCell *cell; cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; [cell.titleLabel setText:[self.dataArray objectAtIndex:(indexPath.row)%self.dataArray.count]]; return cell; 

}

// DidSelect

 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"Index path is %d", indexPath.row%self.dataArray.count); 

}

这将给无限的滚动效果。 要启用无限滚动双方,使用ViewWillAppear中的ScrolltoIndexpath::( ItemCount的中间)! 希望这将有助于正在构build无限滚动UICollectionView的人。