UICollectionview footer in swift

限于喜欢 提交于 2020-01-17 04:58:10

问题


I want to add footer to a UICollectionViewusing this code. i am using Using Custom Layout in UICollectionView layout

Added delegate and datasource methods :-

UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

Code in viewdDidLoad() function :-

 UINib(nibName: "ProfileFooter", bundle:nil)
 collectionView!.registerNib(nibName1, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "ProfileFooter")
        collectionView.registerClass(ProfileFooter.classForCoder(), forSupplementaryViewOfKind: "ProfileFooter", withReuseIdentifier: "ProfileFooter")

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

        var reusable = UICollectionReusableView()

            if kind == UICollectionElementKindSectionFooter{
                let ProfileFooter = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "ProfileFooter", forIndexPath: indexPath)
                ProfileFooter.backgroundColor = UIColor.redColor()
                reusable = ProfileFooter
            }

        return reusable
    }

can any one check what is wrong in this ??


回答1:


You forgot to cast your footer view while dequeuing. Try this.

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind
 {
  case UICollectionElementKindSectionFooter:
      let profileFooter = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "ProfileFooter", forIndexPath: indexPath) as! ProfileFooter
      profileFooter.backgroundColor = UIColor.redColor()
      return profileFooter
  case default:
      assert(false, "Unexpected element kind")
 }
}


来源:https://stackoverflow.com/questions/38929586/uicollectionview-footer-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!