Swift:numberOfSectionsInCollectionView不工作?

时间:2017-06-11 18:33:12

标签: ios swift3 uicollectionviewcell collectionview

好吧,我在这里收集视图时遇到问题。这是我想要实现的 - 一个大MAIN页眉和页脚,然后嵌套在里面需要有一些数字(例如:4)部分都只有HEADERS。像这样:

标题主

------
tiny header 1
------
[a][b][c]

------
tiny header 2
------
[a][b][c]

页脚主

看看类似的答案我首先尝试创建不同的子部分,因为现在我只有所有的集合视图单元格和一个大的页脚和标题。问题是我无论如何,我只剩下1个部分:

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 4
    }

我不知道为什么。我从故事板中获取了我的集合视图,并将其设置为:

@IBOutlet var collectionView: UICollectionView!
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //-44
        self.collectionView.backgroundColor = UIColor.white


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:

            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as IndexPath) as! HeaderCollectionReusableView

           header = headerView
            return headerView

        case UICollectionElementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerCell", for: indexPath as IndexPath) as! FooterCollectionReusableView
            //footerView.center = CGPoint(x: footerView.center.x, y: footerView.center.y + 100)

            return footerView

        default:

            assert(false, "Unexpected element kind")
        }
    }

    // ADD STICKER TO CELL
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stickerCell", for: indexPath as IndexPath) as! StickerCollectionViewCell

        cell.backgroundColor = UIColor.blue

        cell.initSticker(stickerView: stickerPack[indexPath.item])
        cell.stickerName = (stickerPack[indexPath.item].sticker?.localizedDescription)!
        cell.initLabels()

        stickerNames.append(cell.stickerName)
        cell.hasSticker = true;

        //HERE MODIFY SPACING ---------------------------------------
        //then for overflow just increase bottom inset?
        //cell.center = CGPoint(x: cell.center.x, y: cell.center.y+100)

        return cell
    }

我该怎么做?当我有4个元素进入单元格时,为什么我只有1个部分?当我在#34;部分设置"数字项目时到4他们都出现在那里?

我做错了什么?

1 个答案:

答案 0 :(得分:2)

  

问题是无论我有什么,我只剩下一节。

这是因为在Swift 3中你需要删除方法名称的后缀部分,只留下numberOfSections,即

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 4
}