如何每行显示 3 个单元格 UICollectionView [英] How to display 3 cell per row UICollectionView

查看:34
本文介绍了如何每行显示 3 个单元格 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标题,我使用 UICollectionView 来显示图像.我需要每行显示 3 个单元格,例如

解决方案

替换这一行

let width = (bounds.size.width - leftAndRightPaddings)/numberOfItemsPerRow

let width = (bounds.size.width - leftAndRightPaddings*4)/numberOfItemsPerRow

<块引用>

因为您没有考虑两个项目之间的间距 &右插图因此它不会在屏幕中调整间距.

private 让 leftAndRightPaddings: CGFloat = 15.0私有让 numberOfItemsPerRow: CGFloat = 3.0让 bounds = UIScreen.mainScreen().bounds让宽度 = (bounds.size.width - leftAndRightPaddings*(numberOfItemsPerRow+1))/numberOfItemsPerRow让 layout = userDetailCollection.collectionViewLayout 为!UICollectionViewFlowLayoutlayout.itemSize = CGSizeMake(宽度, 宽度)

试试这个代码

As per the header, I'm using UICollectionView to display images. I need to display 3 cells per row, something like how Instagram does it.

Because there are many screen sizes, I'm getting the width of the iPhone screen, then divide it by 3. However, it does not show as per what I need it to be.

Here are my codes inside the viewDidLoad() method:

private let leftAndRightPaddings: CGFloat = 8.0
private let numberOfItemsPerRow: CGFloat = 3.0
private let heightAdjustment: CGFloat = 30.0    

let bounds = UIScreen.mainScreen().bounds
let width = (bounds.size.width - leftAndRightPaddings) / numberOfItemsPerRow
let layout = userDetailCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSizeMake(width, width)

解决方案

Replace this line

let width = (bounds.size.width - leftAndRightPaddings) / numberOfItemsPerRow

with

let width = (bounds.size.width - leftAndRightPaddings*4) / numberOfItemsPerRow

As you are not considering spacing between two items & Right insets spacing therefore it is not adjusting in the screen.

private let leftAndRightPaddings: CGFloat = 15.0
private let numberOfItemsPerRow: CGFloat = 3.0

let bounds = UIScreen.mainScreen().bounds
let width = (bounds.size.width - leftAndRightPaddings*(numberOfItemsPerRow+1)) / numberOfItemsPerRow
let layout = userDetailCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSizeMake(width, width)   

Try this code

这篇关于如何每行显示 3 个单元格 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆