我正在尝试使用无限滚动的方式来实现一个集合视图。滚动应该是循环的,就像在
你觉得呢?
UIPickerView
中一样。这能够使用苹果的 UICollectionView
实现吗?还是只能创建一个水平的 PickerView?你觉得呢?
UIPickerView
中一样。这能够使用苹果的 UICollectionView
实现吗?还是只能创建一个水平的 PickerView?UICollectionView
一起使用。它在你滚动时重新居中,然后将新项目放置在当前可见项目之后(或之前,具体取决于滚动方向)。-(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:(Itemscount的中间)! 希望这能帮助即将构建无限滚动UICollectionView的人。
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);
}
}
UICollectionView
并不是为那种行为设计的,即使你能解决问题,它的性能也会非常差。 - DBD