0

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 ??

Ravi
  • 104
  • 2
  • 14
  • Why have you written both registerNib and registerClass ? remove the line of registerNib. – Hardik Shekhat Aug 13 '16 at 05:53
  • I have checked with both still its not working ... – Ravi Aug 13 '16 at 06:08
  • Write registerClass line like this : `registerClass(ProfileFooter, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "ProfileFooter")` and also check once again for identifier of footer view is correct or not. – Hardik Shekhat Aug 13 '16 at 06:12
  • I have tried that also ... Not working – Ravi Aug 13 '16 at 07:09
  • please see this link : http://stackoverflow.com/questions/33343367/ios-programmatically-collectionview-with-custom-headers and http://stackoverflow.com/questions/29655652/how-to-make-both-header-and-footer-in-collection-view-with-swift – Hardik Shekhat Aug 13 '16 at 07:15

1 Answers1

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")
 }
}