[英]UiCollectionView Spacing issue - swift
我的Collection出现间距问题。我不知道为什么。 我希望从第二行到最后一个低点的图像均匀分布。 有人可以帮我解决这个问题吗? tnx。
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
}
}
视图看起来像这样
实际产量
您需要相应地设置布局,现在您的布局将根据屏幕尺寸进行更改,因此您需要根据一种屏幕尺寸进行计算,然后将其用于其他屏幕尺寸:
现在,布局将像这样。在这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)
}
}
删除单元格和线条的最小间距,截面插图也将为零,如下图所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.