Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Footer

I am trying to add footer to the UICollectionView.

Following is my code,

UICollectionView is added through IB

IN viewDidLoad I register the footer,

[mCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

And implemented following method

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView   viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionFooter) {
      UICollectionReusableView *headerView = [mCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"footer" forIndexPath:indexPath];

      [headerView addSubview:mFooterView];
      reusableview = headerView;
   }
   return reusableview;
}

But My application keep crashing and below is the log,

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2380.17/UICollectionView.m:2249

Any help is appreciated.

Thanks.

like image 532
subhash Amale Avatar asked Jul 26 '13 10:07

subhash Amale


People also ask

How do I add a header in collectionView?

To display this section header, you'll use UICollectionReusableView . This class is like UICollectionViewCell , except it's usually used to display headers and footers. Like cells, UICollectionView places them in a reuse queue rather than deleting them when they scroll out of the visible bounds.

What is viewForSupplementaryElementOfKind?

collectionView(_:viewForSupplementaryElementOfKind:at:)Asks your data source object to provide a supplementary view to display in the collection view.

What is UICollectionReusableView?

A view that defines the behavior for all cells and supplementary views presented by a collection view.

What is swift UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.


1 Answers

in your code why you are dequeing a header view and adding the footer to it ?

the normal implementation of this method is :

- (UICollectionReusableView *)collectionView:(UICollectionView *)theCollectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)theIndexPath 
{

   UICollectionReusableView *theView;

   if(kind == UICollectionElementKindSectionHeader)
   {
      theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:theIndexPath];
   } else {
      theView = [theCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:theIndexPath];
   }

   return theView;
}
like image 153
Boaz Saragossi Avatar answered Sep 18 '22 10:09

Boaz Saragossi