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.
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.