问题
I want to add footer to a UICollectionView
using 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