Swift:NumberOfSectionInCollectionView 不起作用



好的,我在这里的集合视图上遇到了问题。这就是我试图实现的目标 - 1 个大的主页眉和页脚,然后嵌套在其中需要一些数字(例如: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
    }

我该怎么做?为什么当我有 1 个元素要进入单元格时,我只有 4 个部分,而当我将"部分中的数字项目"设置为 4 时,它们都显示在那里?

我做错了什么?

问题是无论我对此有什么,我都只剩下 1 个部分。

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

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

最新更新