Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding UICollectionViewCell

I'm trying to hide a UICollectionViewCell in a collection view. Although I'm successful when I do

cell.hidden = YES; //or cell.alpha = 0.0;

but after a scroll the cell appears again. I've also tried the following:

UICollectionViewLayoutAttributes *layoutAttr = <get the layout attribute>//I'm succesfull here
layout.hidden = YES;
[cell applyLayoutAttributes:layoutAttr];

I thought this might be because I'm using the dequeReusable.. method and hence the cell is being re used, but I have also tried to hide the cell in the collectionView:cellForItemAtIndexPath: method to no avail. Here it doesn't even seem to work.

Why is this not working? How can i hide a UICollectionViewCell?

EDIT: Included the implementation of collectionView:cellForItemAtIndexPath::

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.layer.cornerRadius  = 12.0f;
    cell.backgroundColor = [UIColor colorWithRed:0.830 green:0.899 blue:1.000 alpha:1.000];

    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor lightTextColor];;
    cell.layer.borderColor = [UIColor blackColor].CGColor;
    cell.layer.borderWidth = 2.0f;
    cell.hidden = YES;
    UILabel *lbl = (UILabel *)[cell viewWithTag:10];
    NSArray *interArray = numberArray[indexPath.section];
    [lbl setText:[interArray[indexPath.row] stringValue]];

    return cell;



}

This should hide all the cells right? But nope, it doesn't happen.

like image 274
Rakesh Avatar asked Jul 15 '13 18:07

Rakesh


4 Answers

Since hiding the cell itself doesn't seem to work, you can either add a subview to the cell with the same color as the collection view's background color, or you can hide the cell's content view, which does work:

cell.contentView.hidden = YES;
cell.backgroundColor = self.collectionView.backgroundColor;

That second line is only necessary if you have set a background color for your cell.

like image 71
rdelmar Avatar answered Nov 04 '22 12:11

rdelmar


There is a simple way to do it:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
    CGSize retval;
......
//for the cell you want to hide: 
if(hidden_flag) 
  retval = CGSizeZero;
else
   retval = CGSizeMake(320,50);
return retal; 

You can check a flag in the above function, if the flag is set, you return zero sized value; if not you return the normal value. There is no change needed in any other delegate/data source methods. The flag can be set/reset in other places. Once the flag changed, you need call:

hidden_flag = YES; 
[self.collectionView reloadData];

There is no need to change cellForItemAtIndexPath.

like image 27
us_david Avatar answered Nov 04 '22 10:11

us_david


Because when your cell is scrolled out of view it gets recycled. When scrolled back into view the collection view (or table view) will recreate it by calling cellForItemAtIndexPath. So, when this is called for the cell that should be hidden you will have to hide it again.

It's possible that after you return the cell from collectionView:cellForItemAtIndexPath: that the framework is calling [setHidden:NO] on it. You may have to subclass UICollectionViewCell and provide your own implementation to accomplish this. Or add a subview to the UICollectionViewCell to contain your content, then hide/show this subview as needed.

like image 2
Nicholas Hart Avatar answered Nov 04 '22 11:11

Nicholas Hart


I know this question is old, but for future developers using iOS 8+ it's possible to hide the cell in the willDisplayCell method.

For exemple, this code hide cells in collectionView vertical center.

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Hide cell at center
    CGPoint centerCellOffset = collectionView.contentOffset;
    centerCellOffset.x += collectionView.frame.size.width / 2;
    BOOL isCenterCell = CGRectContainsPoint(cell.frame, centerCellOffset);

    cell.hidden = isCenterCell;
}

(PS : Tested on iOS 10.3)

like image 1
vmeyer Avatar answered Nov 04 '22 10:11

vmeyer