UiCollectionView Spacing问题 - swift

时间:2018-06-03 05:57:59

标签: ios swift xcode uicollectionview

我的Collection.i有间距问题无法找出原因。我希望从第二行到最后一行的图像等间距。有人可以帮我解决这个问题。 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
    }
}

查看外观像这样

enter image description here

实际输出

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要相应地设置布局,现在您的布局将根据屏幕大小进行更改因此您需要根据一个屏幕尺寸进行计算,然后它将适用于其他屏幕尺寸:

现在,布局将是这样的,在这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