集合视图中的标题不会显示?曾经工作?

时间:2019-07-31 01:26:21

标签: ios swift uicollectionview imessage-extension

我从Swift 3到Swift 5迁移了这个项目(一个具有收集视图以保存贴纸的iMessage应用程序)到Swift 5,一切正常,但是标头视图功能根本不会触发,标头也不会显示。

我在情节提要中注册标题单元格:

enter image description here

然后在集合视图功能中:

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

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

        print("CALLED")
        //header1 = headerView as! UICollectionReusableView

        return headerView
    }

我什至尝试在情节提要中将页眉更改为其他颜色,但是此功能甚至无法打印到控制台。怎么了这曾经起作用,其他单元也起作用。

1 个答案:

答案 0 :(得分:1)

  

我将这个项目(一个具有收集视图以保存贴纸的iMessage应用程序)从Swift 3迁移到了Swift 5

好吧,您没有迁移此行:

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

将其更改为:

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

不是private。不是atIndexPath。不是NSIndexPath。谈论Swift,而不是Objective-C,不要在Objective-C中隐藏此方法,否则Cocoa不能调用它。

另外,请务必确保您位于声明采用UICollectionViewDataSource的位置(您可能是这样,因为您说其他方法被称为可以)。