Hiding UICollectionViewCell

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

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

but after scrolling the cell will reappear. I also tried the following:

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

I thought it could be because I use the dequeReusable.. method and therefore the cell is reused, but I also tried to hide the cell in the collectionView:cellForItemAtIndexPath: method to no avail. It doesn’t even work here.

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

EDIT: 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; } 

Should this hide all cells? But no, this does not happen.

+7
ios objective-c cocoa-touch uicollectionview uicollectionviewcell
source share
4 answers

Since hiding the cell itself does not work, you can either add a subview to the cell with the same color as the background color of the collection, or you can hide the view of the contents of the cell that works:

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

This second line is only needed if you have set the background color for your cell.

+6
source share

There is an easy way to do this:

 - (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 the flag in the above function, if the flag is set, you return a null value; if you do not return the normal value. In any other methods of the delegate / data source, changes are not required. The flag can be set / reset in other places. After changing the flag you need to call:

 hidden_flag = YES; [self.collectionView reloadData]; 

No need to change cellForItemAtIndexPath .

+4
source share

Because when your cell scrolls out of sight, it is recycled. On re-viewing, viewing the collection (or view of the table) recreates it by calling cellForItemAtIndexPath . Therefore, when this is called for a cell to be hidden, you will have to hide it again.

Perhaps, after the cell returns from collectionView:cellForItemAtIndexPath: that the infrastructure calls [setHidden:NO] on it. You may need to subclass UICollectionViewCell and provide your own implementation for this. Or add a subview to the UICollectionViewCell to contain your content, then hide / show this view if necessary.

+2
source share

I know this question is old, but for future developers using iOS 8+, you can hide the cell in the willDisplayCell method.

For example, this code hides cells in the vertical center of collectionView.

 - (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)

+1
source share

All Articles