gpt4 book ai didi

swift - 如何: Center UICollectionViewCells

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:51 24 4
gpt4 key购买 nike

我有一个 Collection View 数据源正在经历 View 更新/并在此过程中多次重新加载其数据。有人可以告诉我如何在完成所有操作后将单元格居中吗?

数据源方法:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.testSlice.count
}

当前方法处理单元格样式并做一些其他事情:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

cell.profilePicture.layer.masksToBounds = true

print("DEV: testSlice value: \(testSlice)")
print("DEV: limitedNames value: \(limitedNames)")

if testSlice != [] {
cell.profilePicture.playPortalProfilePic(forImageId: testSlice[indexPath.item], { error in
if let error = error {
print("Error requesting profile pic: \(String(describing: error))")

if let index = self.testSlice.index(of: "") {
self.testSlice.remove(at: index)
collectionView.reloadData()


}

}
}
)
} else {
print("items array was empty, value: \(items)")
}



cell.backgroundColor = UIColor.cyan
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8

return cell
}

我相信我正在寻找一个善于约束的人,​​除非可以通过其他方式做到这一点。

我正在尝试将显示的标签下的单元格居中

I am trying to center the cells under the label shown

最佳答案

您可以通过调整该部分的插图来使单元格居中(如果您只有一个部分)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

let widthOfCell = 50 // What ever the width of the cell is
let spacingBetweenCells = 10

let totalCellWidth = widthOfCell * self.data.count
let totalSpacingWidth = spacingBetweenCells * (self.data.count - 1)

let leftInset = (collectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2

if leftInset < 0 {
return UIEdgeInsets.zero
}

let rightInset = leftInset

return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}

关于swift - 如何: Center UICollectionViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236095/

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