gpt4 book ai didi

ios - UiCollectionView 间距问题 - swift

转载 作者:行者123 更新时间:2023-11-28 07:47:22 25 4
gpt4 key购买 nike

我的收藏集有间距问题。我不知道为什么。我希望从第二行到最后一个低点的图像间隔相等。有人可以帮我解决这个问题吗?谢谢。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (indexPath.row == 0) {
return singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
} else {
return CGSize(width: self.view.frame.width * 0.4, height: self.view.frame.height / 4)
}
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserDetailCell", for: indexPath) as! UserDetailCell
configureCell(cell: cell, forRowAtIndexPath: indexPath as NSIndexPath)
return cell
}

func configureCell(cell: UserDetailCell, forRowAtIndexPath indexPath: NSIndexPath) {
DispatchQueue.global(qos: .background).async {
cell.ItemImage.image = self.singleUserModel.loadImage(imageUrl: self.CurrentUser.items[indexPath.row])
cell.activityIndicator.isHidden = true
}
}

看起来像这样

enter image description here

实际输出

enter image description here

最佳答案

你需要相应地设置布局,现在你的布局会根据屏幕尺寸改变所以你需要根据一个屏幕尺寸计算然后它会在其他屏幕尺寸上工作:

现在,布局将像这样,在这 2 个图像中将以宽度显示,如果您想从左侧和右侧设置空间,则相应地设置布局宽度。

    func collectionView(_ collectionView: UICollectionView, layout 

collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if (indexPath.row == 0) {
return singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
} else {
return CGSize(width: (self.view.frame.width/2), height: self.view.frame.height / 4)
}
}

删除单元格和线条的最小间距,部分插图也将为零,如下图所示:

enter image description here

关于ios - UiCollectionView 间距问题 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663688/

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