gpt4 book ai didi

ios - 单元格 UICollectionView 之间的间距不正确

转载 作者:行者123 更新时间:2023-11-29 00:07:58 24 4
gpt4 key购买 nike

我有一个包含 UICollectionViewCellsUICollectionView,其中包含用于内容的 UIButton

但是每个单元格之间的间距不正确。 Collection View 中的单元格大小并不具体,因为它应该是长度明显不同的标签列表。

它看起来像这样:

enter image description here

每个单元格之间的水平间距完全不正确,因为它们随行而异。

单元格的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var wordsCollection: UICollectionView!
var items = ["test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection"]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "wordCell", for: indexPath as IndexPath) as! WordCell
cell.wordButton.setTitle(items[indexPath.row], for: UIControlState.normal)
cell.wordButton.backgroundColor = UIColor.gray
if(indexPath.row % 3 == 0){
cell.wordButton.backgroundColor = UIColor.gray
}
cell.wordButton.clipsToBounds = true
cell.wordButton.layer.cornerRadius = 2
cell.wordButton.sizeToFit()

return cell
}

override func viewDidLoad() {
super.viewDidLoad()
if let flowLayout = wordsCollection.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize(width:1, height:1) }
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

语言是 Swift 4,我的目标是 iOS 10.3。

最佳答案

实现:UICollectionViewDelegateFlowLayout

然后:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return THE_SPACING_AMOUNT
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return SPACING_BETWEEN_ROWS
}

关于ios - 单元格 UICollectionView 之间的间距不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47456665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com