在集合视图中一次滚动一个单元格

Nar*_*dey 1 objective-c ios

使用该willDisplayCell方法,我得到了最后一个可见单元格的索引:

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {       
   indexVisibleCell = [indexPath indexAtPosition:[indexPath length] - 1];
}
Run Code Online (Sandbox Code Playgroud)

这里indexVisibleCell是一个NSUInteger是在类的接口中声明。我的问题是,当用户点击向右或向左按钮时,我只希望移动一个单元格。我已经为此目的应用了此代码:

- (IBAction)rightButton:(id)sender {
    NSUInteger indexOfScrolledCell;
    indexOfScrolledCell = indexVisibleCell;

  if (indexVisibleCell < 9) {            
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexOfScrolledCell+1 inSection:0];

        [self.obj_CollectionView1 scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];            
    }     
}
Run Code Online (Sandbox Code Playgroud)

因为我一次显示3个单元格。通过使用rightButton动作方法,它一次移动3个单元格,而不是一个。请帮助我弄清楚如何一次滚动一个单元格。

Aam*_*mir 5

尝试UICollectionView在启用分页的情况下使用。使用以下代码,希望它对您有用。

[yourCollectionView setPagingEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

  • 启用分页后,他的集合视图(一次显示3个单元格)将每3个单元格滚动一次,这不是他一次需要的1个单元格 (2认同)