ios - UICollectionview 和单元格

标签 ios iphone objective-c uicollectionview

我第一次使用 Collection View ,我不知道该怎么做..

我有一个包含 12 个单元格的 UICollectionView。我将 collectView 设置为仅水平滚动,并且单元格彼此相邻排列。我还打开了分页,因此我可以使用 UIPageControll 来指示滚动处于事件状态。

我希望 Collection View 在任何时候都只在屏幕上显示四个单元格。当 View 加载时,我得到四个单元格,没问题。但是,当我水平滚动时,我得到 4 个半单元格。永远不会只有四个。

有没有办法告诉 Collection View 一次只显示四个单元格?

最佳答案

enter image description here

enter image description here

如果不需要动态,您可以在 Collection View 中静态添加单元格(项目)的数量。

在这里我使用滚动方向水平,你可以在垂直方向上做同样的事情。
希望这会有所帮助

你也可以这样做。只需将此代码复制到您的 View Controller 中并进行一些更改。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

   NSIndexPath *indexPath;
    for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
        indexPath = [self.collectionView indexPathForCell:cell];
        NSLog(@"%@",indexPath);
    }

    UICollectionViewCell *cell =(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

    //finally get the rect for the cell
    CGRect cellRect = cell.frame;


    self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, cellRect.origin.y);
}

关于ios - UICollectionview 和单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878653/

相关文章:

iphone - 从命令行分发 ipa 到 iTunes connect

iphone - 为什么这个NSMutableSet不会泄漏内存

objective-c - 关于使用 Objective C 编写更多模块化和可测试的 iOS 应用程序的建议

objective-c - RootViewController 切换过渡动画

ios - 我有两个 if 语句,第一个被跳过,为什么?

ios - 如何在 iOS 上调整 OpenGL ES 渲染缓冲区的大小?

IOS:UITableView 中的多个 PrototypeCells

iphone - UIViewController中的"[CALayer release]: message sent to deallocated instance"

ios - 如何从单独的 uiviewcontroller 刷新 TableView ?

ios - 导航 Controller 中的 UIViewController 并以模态方式呈现