Ravi
Ravi

Reputation: 104

UICollectionview footer in swift

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

Upvotes: 0

Views: 2602

Answers (1)

Chaithanya Prathyush
Chaithanya Prathyush

Reputation: 311

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

Upvotes: 1

Related Questions