collectionView footer not showing - ios

I've checked the section footer as shown in the screenshot below.
I've also implemented the dataSource method:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
var header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as? DetailHeader
if header == nil {
header = DetailHeader()
}
return header!
} else {
println("footer")
var footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView
return footer
}
}
I was able to display my header, but my footer is never displayed. The println never triggers either.

Screen shot from storyboard:
2.My code:
import UIKit
class BaseCVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cvcCell", forIndexPath: indexPath)
return cell;
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Int(2)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSizeMake(self.view.frame.size.width, 20)
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
return header
} else {
print("footer")
let footer = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath)
return footer
}
}
}
Result in simulator:
Important note: I've used Xcode 7 beta 5 (Swift 2.0)

Option 1: Set the footerReferenceSizeon your UICollectionViewDelegateFlowLayout greater than 0
Option 2: Implement the collectionView(_:layout:referenceSizeForFooterInSection:)
function in you UICollectionViewDelegateFlowLayout.

Drag and drop
CollectionReusableView
for both header and footer in
UICollectionViewController
from storyBoard and give identifier for each. The same process you drag and drop UITableViewCell in UITableView and give cell identifier.
Good luck

Related

Handle moveItem(at:to:) delegate method when using UICollectionViewDiffableDataSource

I need to use moveItem(at:to:) delegate method in order to move my collection view cells.
When I implement the data source delegation methods it's working fine, but I need to set my data source as UICollectionViewDiffableDataSource and in this way the moveItem(at:to:) did not call.
Any solution to handle,
This way it's work when using delegations :
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
cell.textLabel.text = "Some value"
return cell
}
override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
true
}
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print("OK")
}
set data source in this way is not work
func setupDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, Person>(collectionView: collectionView, cellProvider: {
(collectionView: UICollectionView, indexPath: IndexPath, person: Person) -> UICollectionViewCell? in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
cell.textLabel.text = "Some text"
return cell
})
}
For custom behavior create a subclass of UICollectionViewDiffableDataSource and override the methods there.

How to call the indexPath.row of a UITableViewCell with a UICollectionView embedded within

Currently, I have a UITableView with a UICollectionView embedded in it. I have 3 arrays of information and I want each array to correspond to a UICollectionView, and therefore a UITableViewCell.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CookieCell", for: indexPath) as? MenuCollectionViewCell
cell?.fullView.layer.borderColor = UIColor.black.cgColor
cell?.labelView.layer.borderColor = UIColor.black.cgColor
cell?.fullView.layer.borderWidth = 0.3
cell?.labelView.layer.borderWidth = 0.3
return cell!
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
Where each of the return cell? is, I want to do something like:
if indexPathOfTableView.row == 0 {
//designated parameters
}
else if indexPathOfTableView.row == 1 {
//designated parameters
}
else {
//designated parameters
}
How would I do this?
Hello #helloworld12345
from your question I got the idea that you want to collectionview in tableviewcell
may be below code will help you.
first in you viewController or tableviewcontroller,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//send indexPath of this cell
let cell = tblView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! tableViewCell
//Pass your array which you want to show in collectionViewCell and then reload collection view
cell.clnView.reloadData()
return cell
}
In your tableViewCell swift file:
class tableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
#IBOutlet weak var clnView: UICollectionView!
//after awakenib method write below code
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 4 //pass your array count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let flowayout = collectionViewLayout as? UICollectionViewFlowLayout
let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0)
let size:CGFloat = (self.clnView.frame.size.width - space) / 2.0 //By this you can show two cell in one screen
return CGSize(width: size + 60, height: (size + 200))
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "clnViewCell", for: indexPath) as! clnViewCell
// do your stuff with your
return cell
}
}

IOS, swift - UICollectionReusableView not working

I want to make collection view header but it isn't working.
Storyboard
On device
My code below is not working, print("ssssss") don't show.
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
print("sssssss")
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "mypageheader",
for: indexPath) as! MyPageHeaderView
headerView.backgroundColor = UIColor.blue;
return headerView
default:
assert(false, "Unexpected element kind")
}
}
on viewDidLoad()
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")
You should implement this delegate method:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 100)
}
Don't forget to setup the delegate of your collectionView in the storyboard!
If you are configuring custom view via .xib/storyboard, you don't need to add the following method in the viewDidload:
self.collectionView?.register(MyPageViewController.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: "mypageheader")
Select the view in storyboard and Go to the identity inspector, and link the Class nad Module
Then go to the attribute inspector and ann the identifier:
And make sure to add the method in your code:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HomeListTopCollectionReusableViewId", for: indexPath) as! HomeListTopCollectionReusableView
return header
}

Display Section Header UICollectionReusableView

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!
}

iOS Swift - UICollectionView - Sections different background color

Is there a way to set different background colors to UICollectionView's header and the content view?
I want my collection's header background color - clearColor and the content background to be white.
Thank you!
You can change the UICollectionViewCell background color within cellForItemAtIndexPath and the section header background within viewForSupplementaryElementOfKind
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.backgroundColor = UIColor.magentaColor()
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.whiteColor()
return cell
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 10
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
if kind == UICollectionElementKindSectionHeader {
headerView.backgroundColor = UIColor.clearColor()
}
return headerView
}

Resources