Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collectionView footer not showing

Tags:

ios

swift

I've checked the section footer as shown in the screenshot below.

collectionViewAttributesInspector

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.

like image 829
Kelvin Lau Avatar asked Aug 15 '15 10:08

Kelvin Lau


People also ask

What is the difference between header view and footer view?

The header view is designed to display a section title, while the footer view only shows a static banner image. In Storyboard, drag an image view from Object Library to the header view and add a label on top of it. Set the font colour to white. For the footer, simply add an image view.

What is a collectionview?

The .NET Multi-platform App UI (.NET MAUI) CollectionView is a view for presenting lists of data using different layout specifications. It aims to provide a more flexible, and performant alternative to ListView.

How to configure header and footer views of the flow layout?

Here the header and footer views can be referred as the supplementary views of the flow layout. By default, these views are disabled in the flow layout. There are a couple of things to configure the header/footer view: Enable the section header/footer view in Storyboard (We try to keep thing simple.

How do I add a header and footer in Xcode?

First, download the header/footer background images and add them into the Xcode project. Go back to Storyboard. Select “Collection View” of the Collection View controller. In Attributes inspector, select both “Section Header” and “Section Footer” for Accessories.


1 Answers

  1. Screen shot from storyboard: enter image description here

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
        }
    }
}
  1. Result in simulator:

enter image description here

  1. Important note: I've used Xcode 7 beta 5 (Swift 2.0)
like image 135
Alexey Bondarchuk Avatar answered Sep 22 '22 02:09

Alexey Bondarchuk