【问题标题】:Paging UICollectionView Cell分页 UICollectionView 单元格
【发布时间】:2018-02-27 07:30:34
【问题描述】:

我正在构建一个应用程序,其中 UICollectionView 位于 UITableViewCell 上,这里我希望 CollectionView 应该水平滚动,这正在发生,但我需要在这里它像一个单元格一样在固定位置一次分页,但我在这里得到的是通过水平滚动它滚动但前一个单元格显示很少,并且显示部分在传递每个单元格时变得更多。

这是我制作的Demo,请通过链接告诉我我做错了什么,以及如何做才能得到我想要的结果。

链接:https://drive.google.com/file/d/13k3JyaWiq1FuzGK4_8WNguXTUYwb-Xu6/view?usp=sharing

谢谢,任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift uitableview uicollectionview uicollectionviewlayout


    【解决方案1】:

    对于这个实现,UICollectionViewDelegateFlowLayout 方法

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        let width  = (collectionView.frame.size.width)
        return CGSize(width: width, height: your tableview height here.. or collectionview cell height)
    
    }
    

    也在包含您的collectionview的tableview单元格的interfacebuilder中,选择collectionview并将行和单元格的最小间距设置为0,并将部分插入也设置为0

    并确保在界面生成器中您已将滚动方向设置为水平方向并为 collectionview 启用分页

    【讨论】:

    • 不,它不能这样工作..改变每个单元格的颜色你会得到它..
    • 我确实进行了更改并且工作正常,因为它应该可以工作.. drive.google.com/open?id=14u7q2MTd1jX-mv3kI21t9uh1ZxZN0X8U 这不是你想要的.. 吗?保持冷静并检查可能出错的地方以及单元格背景颜色,我使用了 func getRandomColor() -> UIColor{ let randomRed:CGFloat = CGFloat(drand48()) let randomGreen:CGFloat = CGFloat(drand48()) let randomBlue:CGFloat = CGFloat(drand48()) return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0) }
    • 好的,我要投票给你了。请看我的回答,它现在就像魅力一样。无论如何,谢谢。
    【解决方案2】:

    嗯,我找到了答案,通过把 UICollectionViewDelegateFlowLayout 的这个代表方法

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 10
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: collectionView.frame.size.width - 10, height: collectionView.frame.size.height)
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
            return UIEdgeInsetsMake(0, 5, 0, 5)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2021-07-10
      • 2023-03-27
      相关资源
      最近更新 更多