1

我有一个 UICollectionView 包含两个部分。我从情节提要中给出了 left = 5 和 right = 5 的部分插图。我对这两个部分的单元格使用相同的 .xib。我正在输入单元格的大小sizeForItemAtIndexPath

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        let itemWidth = self.collectionViewTrending.bounds.size.width - 10
        if(indexPath.section == 0) {

                return CGSizeMake(itemWidth , 200)

        } else {

            let product = self.products.objectAtIndex(indexPath.row) as! Product
            let imageHeight = CGFloat(product.imageHeight)
            return CGSizeMake(itemWidth, imageHeight * 0.81)
        }



   }

问题是这样的:

对于第 0 部分,单元格居中并尊重部分插图。

对于第 1 节,左侧部分插图受到尊重,但右侧不被尊重,并且第 1 节的所有单元格的右边缘都与 collectionView 的右边缘接触。

为什么会发生这种情况,我该如何解决?

4

1 回答 1

1

Section insets apply to the section as a whole:

The left inset affects the space between the left edge of the section and the left edge of the FIRST cell in that section.

The right inset affects the space between the right edge of the LAST cell in the section and the right edge of that section.

In other words, the behavior you're seeing is probably because you have only one cell in section 0, and multiple cells in section 1.

What you probably want is space between cells to create the appearance of a border around each cell.

To do this, set the Min Spacing / For Cells attribute of the collection view in your storyboard. Or set this dynamically in the collectionView minimumInteritemSpacingForSectionAtIndex delegate method.

于 2016-05-12T15:45:48.917 回答