代码之家  ›  专栏  ›  技术社区  ›  Sathya Baman

uiCollectionView间隔问题-swift

  •  1
  • Sathya Baman  · 技术社区  · 7 年前

    我的收藏有间隔问题。我不知道为什么。我希望从第二排到最后一排的图像间隔相等。有人能帮我修一下吗?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 回复  |  直到 7 年前
        1
  •  0
  •   Jogendar Choudhary    7 年前

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

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