6
  1. insetForSectionAtIndex(在 DelegateFlowLayout 上)允许为一个部分中的所有单元格设置插图

  2. sectionInset(在 FlowLayout 上)使您能够设置适用于所有部分的插图。

但是,我正在寻找一种仅将插图应用于一个特定部分的方法 - 这可能吗?

4

3 回答 3

7

你必须UICollectionViewDelegateFlowLayout用这个方法来实现你的类:

对于斯威夫特 4

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;
}

对于斯威夫特 3

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;

}
于 2016-08-09T20:01:15.073 回答
1

对于 swift 4 被命名为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
...
于 2018-04-11T14:05:38.907 回答
1

UICollectionViewDelegateFlowLayout你会从协议中做这样的事情:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {

        switch section {
        case 0:
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        case 1:
            return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        }
    }
于 2020-06-18T19:39:39.097 回答