ios - swift : numberOfSectionsInCollectionView not working?

标签 ios swift3 uicollectionviewcell collectionview

好的,我在这里的收藏 View 有问题。这就是我试图实现的 - 1 个大的 MAIN 页眉和页脚,然后嵌套在里面需要一些数字(例如:4)部分,所有部分都只有 HEADERS。像这样:

标题主要

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

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

页脚主要

查看类似的答案,我首先尝试创建不同的小节,因为现在我只有所有的 Collection View 单元格和一个大的页脚和页眉。问题是无论我有什么,我只剩下 1 个部分:
 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 4
    }

我不知道为什么。我从 Storyboard 中获取我的收藏 View 并将其设置如下:
@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 个部分,而当我将“部分中的项目数”设置为 4 时,它们都显示在那里?

我究竟做错了什么?

最佳答案

Problem is no matter what I have for this, I remain with only 1 section.



这是因为在 Swift 3 中你需要去掉方法名的后缀部分,只留下 numberOfSections , IE。
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 4
}

关于ios - swift : numberOfSectionsInCollectionView not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487307/

相关文章:

ios - 替换父 View Controller

ios - 滑动以显示 UICollectionView 上的其他按钮

swift3 - Swift 中的 Error 和 NSError 有什么区别?

ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度

ios - 格式化整数值

ios - 如何从磁盘上的视频文件中获取 NSData

ios - 选择表格中的单元格无法正常工作

ios - 如何使用 Swift 3 获取我当前的起始位置并测量运行后/运行期间的距离

ios - 如何为 UICollectionView 单元格选择设置动画

ios - 显示具有多种不同 UICollectionViewCells 类型的 UICollectionView