I'm attempting to implement a collection view with an infinite scrolling behaviour. The scrolling should be circular - like in a UIPickerView
. Can it be done with Apple's UICollectionView
or there is no choice but to create a horizontal PickerView ?
What do you think?
You should watch the WWDC 2011 session video "Advanced ScrollView Techniques", they talk about infinite scrolling, and I'm pretty sure it's possible to use that with UICollectionView
s. It recenters while you scroll, and places the new items after the current visible items (or before, depending on the scroll direction).
Like they say in the session, you should not use an approach that has an ending. Like stated in the session: People will definitely find the edge at one point.
Override this method to enable infinite scrolling both ways. It center content whenever it is too far from the center and user never hits the end.
-(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);
}
}
Yes, you can do this. Return a very large number for collectionView:numberOfItemsInSection:, and in collectionView:cellForItemAtIndexPath:, you use the mod operator to always return a number for indexPath.Row that's between 0 and the last index in your data array. So if you had 20 items in your array, you would do something like this:
item.whatever.text = [self.theData objectAtIndex:indexPath.row %20];
Thanks to rdelmar for a tricky solution. That idea of "large number for collectionView:numberOfItemsInSection" worked like a charm. I have tried to implement the same with UIScrollView delegate methods. But the output was jerky.
Here's some part of my code.
-(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);
}
This will give infinite scrolling effect. To enable infinite scrolling both sides, use ScrolltoIndexpath:(middle of Itemscount) in ViewWillAppear !! Hope this will help someone who are about to build an infinite scrolling UICollectionView.
来源:https://stackoverflow.com/questions/15529747/uicollectionview-with-infinite-scrolling