~New to Swift~
Below is the func I have to implement header and footer views into my UICollectionView but as of now when I run my app nothing is showing up.
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath)
headerView.backgroundColor = UIColor.brownColor()
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath)
footerView.backgroundColor = UIColor.darkGrayColor()
return footerView
default:
assert(false, "Unexpected element kind")
}
}
Related
I have a header and a footer in my CollectionView and I want both of them to be displayed. At the current state, I get 2x the Header, because the "kind" of the Section does not automatically change and it enters the case "UICollectionElementKindSectionHeader" twice.
I could switch the indexPath instead of the kind but I want to know the other approach aswell.
func setupCollectionView() {
collectionView?.backgroundColor = .white
collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ProfileHeader.reuseIdentifier)
collectionView?.register(ProfileFooter.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: ProfileFooter.reuseIdentifier)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileHeader.reuseIdentifier, for: indexPath) as! ProfileHeader
header.user = self.user
return header
case UICollectionElementKindSectionFooter:
let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileFooter.reuseIdentifier, for: indexPath) as! ProfileFooter
return footer
default:
assert(false, "Unexpected element kind")
}
}
The reason was that I forgot to define the reference size of the footer. Without it, the kind is not perceived by the application.
I was working on iOS application and I have several problem about using UICollectionView cell.
This time, I want to ask about how to display the section header of UICollectionView (UICollectionReusableView)
I already implement the function like below :
public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
var labelHeader = headerView.viewWithTag(2) as! UILabel
if indexPath.section == 0 {
labelHeader.text = "Specialist Clinic"
}
else {
labelHeader.text = "Medical Support"
}
headerView.backgroundColor = UIColor.blue;
return headerView
default:
assert(false, "Unexpected element kind")
}
}
but, it always give a blank result. please look at the screen shot below
You need to return size of header.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 123) // you can change sizing here
}
Delegate method
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusableview: UICollectionReusableView? = nil
if kind == UICollectionElementKindSectionHeader {
reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHeader is your identifier
var labelHeader = reusableview.viewWithTag(2) as! UILabel
if indexPath.section == 0 {
labelHeader.text = "Specialist Clinic"
}
else {
labelHeader.text = "Medical Support"
}
headerView.backgroundColor = UIColor.blue;
}
return reusableview!
}
I have created demo for you. Download and re-use into your code. Cheers!
Download Link : https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0
You need to add UILabel on headerView
public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
var labelHeader = headerView.viewWithTag(2) as! UILabel
if indexPath.section == 0 {
labelHeader.text = "Specialist Clinic"
}
else {
labelHeader.text = "Medical Support"
}
headerView.backgroundColor = UIColor.blue;
headerView.addSubview(labelHeader) //Add UILabel on HeaderView
return headerView
default:
assert(false, "Unexpected element kind")
}
}
Here is my solution. It's done similar to dequeueing a cell/row for indexPathAt.
CollectionView
// size of header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
CGSize(width: collectionView.frame.size.width, height: 123)
}
// content of header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CollectionViewHeader.identifier, for: indexPath) as? CollectionViewHeader else {
return UICollectionReusableView()
}
headerCell.titleLabel.text = "Title"
return headerCell
}
UICollectionReusableView subclass
class CollectionViewHeader: UICollectionReusableView {
let identifier = "headerView"
#IBOutlet weak var titleLabel: UILabel!
}
I have one button in header cell that created by IB. I add on action, but I can not trigger. This code;
func collectionView( _ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! ProfilePhotoHeaderCell
cell.lblFavCount.text = user.favdCount
let countFoto = ((user.photos?.count)!)
cell.lblFotoCount.text = countFoto.description
cell.btnAddPhotos.isUserInteractionEnabled = true
cell.btnAddPhotos?.addTarget(self, action: #selector(ProfileController.myAction(sender:)), for: .touchUpInside) // add selector
return cell
case IOStickyHeaderParallaxHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell1", for: indexPath) as! ProfileParallaxHeaderCell
return headerView
default:
assert(false, "Unexpected element kind")
}
}
I want to add a few buttons to the header of a Collection view. Enabled 'Section Header' from the storyboard. Then dragged a 'view' to that header. And put its background color to green
But I don't why it's not displaying. In the simulator as you see, only a blank space for the header.
Use this Method
Give name in CollectionHeaderCell
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath as IndexPath)
headerView.backgroundColor = UIColor.blue;
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath as IndexPath)
footerView.backgroundColor = UIColor.green;
return footerView
default:
assert(false, "Unexpected element kind")
}
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath)
let profileimage: UIImageView = (cell.viewWithTag(5) as! UIImageView)
profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!)
return cell
}
I have added the delegate function in the controller class but it won't called at the run time.
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView
headerView.backgroundColor = UIColor.blueColor();
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView
footerView.backgroundColor = UIColor.greenColor();
return footerView
default:
assert(false, "Unexpected element kind")
}
}
I follow this tutorial Appcoda tutorial and update code For swift 4.0
Register the class :
self.collectionView.register(HeadView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeadView")
Delegates :
// For header size (required)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width:collectionView.frame.size.width, height:50.0)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeadView", for: indexPath)
headerView.backgroundColor = UIColor.blue;
return headerView
default:
fatalError("Unexpected element kind")
}
}
Delegate method not called because you need to register a class for the supplementary view;
yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionHeaderID")
yourCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "collectionFooterID")
If you use Custom UICollectionViewFlowLayout
set headerReferenceSize
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.minimumLineSpacing = 0
flowLayout.minimumInteritemSpacing = 0
flowLayout.sectionHeadersPinToVisibleBounds = true
flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.size.width, height: 50)