4

I'm trying to reload only data section, not header or footer (supplementary view), of UICollectionView.

When I use reloadData method, header and footer section also reloads, so that's what I want.

I found the method reloadSections:, but I don't know why it doesn't work.

Because my collection view only contains one section, so I tried using the method like this:

[collectionViewController.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];

But it makes runtime error when the method is called. The content of error is:

'NSInvalidArgumentException', reason: '-[UICollectionViewUpdateItem action]:
unrecognized selector sent to instance 0x90aec00'

Is the method reloadSections: is not allowed to use like this? Is there any alternative way to reload only data section, not header or footer section?

glast
  • 383
  • 1
  • 4
  • 17
  • Nope, this is valid. Look at Apple's docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionView_class/Reference/Reference.html Where is the collectionViewController? Is it on the main view? Do you have to use 'self.collectionView' instead? – Firo Jan 29 '13 at 01:31
  • I made a root view controller and set the UICollectionViewController object, whose name is collectionViewController, as a property of the root view controller. – glast Jan 30 '13 at 11:02
  • I checked that table view's reloadSections:withRowAnimation: method works fine. Are there methods to implement or environments to configure before using collection view's reloadSections method? – glast Jan 30 '13 at 11:11
  • I think this problem is related with [this SO article](http://stackoverflow.com/questions/12894172/deleting-cells-from-uicollectionview-via-nsnotification) because I can't find the same problem when the keyboard is down. But using that fix doesn't work for me... (but got a different error message... "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: <_UICollectionViewItemKey: 0x8c84090> Type = SV Kind = UICollectionElementKindSectionHeader IndexPath = 2 indexes [0, 0])'") – glast Jan 30 '13 at 11:57

1 Answers1

6

I think the error only occurs when the search bar is a collection view's subview, and trying to call reloadSections: when the keyboard input is up.

This article gives an answer to a similar problem, but that didn't work for me.

So I used an alternative way with which I can invoke almost the same feature.

Instead of adding the header as a supplementary view, I made the header a subview of 'view'. (not collectionView)

And added some scroll delegate to mimic the table view's header.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < 44) {
        [self.searchBar setFrame:CGRectMake(0, -y, 320, 44)];
        [self.searchBar setHidden:NO];
    } else if (y >= 44) {
        [self.searchBar setHidden:YES];
    }  
} 
Community
  • 1
  • 1
glast
  • 383
  • 1
  • 4
  • 17
  • +100 Great stuff, just works. There is probably a better solution using the collection view header, but thanks for this anyway :) – Tomasz May 05 '13 at 02:35