UICollectionView:删除单元格之间的空间(每行 7 个项目) [英] UICollectionView: remove space between cells (with 7 items per row)
问题描述
我有一个 UICollectionView
,每行有 7 个项目(或者实际上,每个项目的宽度是 collectionView.bounds.width
除以 7).
I have a UICollectionView
with 7 items per row (or actually, the width of each item is the collectionView.bounds.width
divided by 7).
这是一个代码示例:
final class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let randomColors: [UIColor] = [.red, .blue, .green, .yellow, .orange, .purple, .cyan, .gray, .darkGray, .lightGray, .magenta]
var colors: [UIColor] {
var result: [UIColor] = []
for _ in 0..<200 {
result.append(randomColors[Int(arc4random_uniform(UInt32(randomColors.count - 1)))])
}
return result
}
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width / 7, height: 45)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return .leastNormalMagnitude
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return .leastNormalMagnitude
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: .leastNormalMagnitude, left: .leastNormalMagnitude, bottom: .leastNormalMagnitude, right: .leastNormalMagnitude)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return colors.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColorCollectionViewCell", for: indexPath) as! ColorCollectionViewCell
cell.color = colors[indexPath.row]
return cell
}
}
final class ColorCollectionViewCell: UICollectionViewCell {
var color: UIColor? {
didSet {
self.backgroundColor = color
}
}
}
还有故事板:
由于将 collectionView
的宽度除以 7 通常会产生浮动结果,这会导致某些单元格之间出现空格:
Since dividing the collectionView
's width by 7 often gives a floating result, this results in spaces appearing between some cells:
我可以在这里做什么?
感谢您的帮助.
推荐答案
你必须以某种方式分配额外的像素.不用担心所有单元格的宽度不会完全相同,1px 的差异不会可见:
You have to somehow distribute the additional pixels. Don't worry that all cells won't have exactly the same width, 1px difference won't be visible:
let numColumns = 7
let availableWidth = collectionView.bounds.size.width
let minWidth = floor(availableWidth / CGFloat(numColumns))
let remainder = availableWidth - minWidth * CGFloat(numColumns)
现在如何处理剩余部分?让我们从左侧向单元格添加像素:
Now what to do with the remainder? Let's just add pixels to cell from the left:
let columnIndex = indexPath.row % numColumns
let cellWidth = CGFloat(columnIndex) < remainder ? minWidth + 1 : minWidth
return CGSize(width: cellWidth, height: 45)
这只是基本概念.
这篇关于UICollectionView:删除单元格之间的空间(每行 7 个项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!