【问题标题】:UICollectionView showing 2 items instead of 3UICollectionView 显示 2 个项目而不是 3 个
【发布时间】:2017-08-29 11:06:17
【问题描述】:

我已经像这样实现了我的UICollectionView

extension MyViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath)
        cell.backgroundColor = .black
        return cell
    }
}

extension MyViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let widthPerItem = view.frame.width / 3
        return CGSize(width: widthPerItem, height: widthPerItem)
    }
}

但不是连续 3 个项目,我得到 2 个项目

我该如何解决?我没有为我的项目和部分设置任何UIEdgeInset

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewdelegateflowlayout


    【解决方案1】:

    试试这个:

    extension MyViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            let numRowItems:CGFloat = 3
            let padding:CGFloat = 2
            let width = (collectionView.bounds.width / numRowItems) - padding 
            let height = collectionView.bounds.height - (2 * padding)
            return CGSize(width: width, height: height)
        }
    }
    

    【讨论】:

      【解决方案2】:

      您忘记考虑 UICollectionViewFlowLayoutminimumInteritemSpacingminimumLineSpacing 属性 - 如果您没有设置它们,它们都默认为 10.0

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
          let spacing : CGFloat = (collectionViewLayout as? UICollectionViewFlowLayout)?.minimumInteritemSpacing ?? 0.0
          let widthPerItem = (view.frame.width  - spacing * 2)/ 3
          return CGSize(width: widthPerItem, height: widthPerItem)
      }
      

      【讨论】:

        【解决方案3】:

        这是因为您的屏幕宽度不能容纳 3 个单元格。这是因为如果您尝试在 sizeForItemAt 中使用小宽度,那么集合视图在您的故事板中具有单元格最小行距属性。

          func collectionView(_ collectionView: UICollectionView,
                                layout collectionViewLayout: UICollectionViewLayout,
                                sizeForItemAt indexPath: IndexPath) -> CGSize {
                let widthPerItem = view.frame.width / 3 - ((collectionViewLayout as? UICollectionViewFlowLayout)?.minimumInterItemSpacing ?? 0.0)
                return CGSize(width: widthPerItem, height: widthPerItem)
            }
        

        【讨论】:

        • 在您的方法 sizeForItemAt 中设置其他最小尺寸。更改此大小,它将起作用。因为如果您实现此方法,则此方法中的值比情节提要中的值更受欢迎
        • 这还能编译吗?您正在尝试从 CGFloat 中减去 Optional<CGFloat>
        【解决方案4】:

        在 obj c 中

         - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        
        //If you want your cell should be square in size the return the equal height and width, and make sure you deduct the Section Inset from it.
        return CGSizeMake((self.view.frame.size.width/2) - 16, (self.view.frame.size.width/2) - 16);
        }
        

        迅速

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // Compute the dimension of a cell for an NxN layout with space S between
        // cells.  Take the collection view's width, subtract (N-1)*S points for
        // the spaces between the cells, and then divide by N to find the final
        // dimension for the cell's width and height.
        
        let cellsAcross: CGFloat = 3
        let spaceBetweenCells: CGFloat = 1
        let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
        return CGSize(width: dim, height: dim)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-17
          • 1970-01-01
          相关资源
          最近更新 更多