0

我已经实现了我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为我的项目和部分设置任何内容

4

4 回答 4

1

你忘了考虑minimumInteritemSpacingminimumLineSpacing属性-如果你没有设置它们UICollectionViewFlowLayout,它们都默认为。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)
}
于 2017-08-29T11:11:17.867 回答
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)
    }
}
于 2017-08-29T11:13:03.427 回答
0

这是因为您的屏幕宽度不能容纳 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)
    }
于 2017-08-29T11:10:04.687 回答
-1

在 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)
}
于 2017-08-29T12:02:23.077 回答