我正在尝试在集合视图中隐藏UICollectionViewCell
。尽管当我这样做的时候我是成功的
cell.hidden = YES; //or cell.alpha = 0.0;
但在滚动之后,单元格再次出现。我还尝试了以下几种方法:
UICollectionViewLayoutAttributes *layoutAttr = <get the layout attribute>//I'm succesfull here
layout.hidden = YES;
[cell applyLayoutAttributes:layoutAttr];
我认为这可能是因为我使用了dequeReusable..
方法,因此该单元格被重用,但我也尝试在collectionView:cellForItemAtIndexPath:
方法中隐藏该单元格,但无济于事。在这里,它甚至看起来都不起作用。
为什么这不起作用?如何隐藏UICollectionViewCell
?
编辑:包含了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;
}
这应该会隐藏所有的单元格,对吗?但事实并非如此。
发布于 2013-07-16 03:49:42
由于隐藏单元格本身似乎不起作用,因此您可以使用与集合视图的背景颜色相同的颜色向单元格添加一个子视图,也可以隐藏单元格的内容视图,这是可行的:
cell.contentView.hidden = YES;
cell.backgroundColor = self.collectionView.backgroundColor;
仅当您为单元格设置了背景色时,才需要第二行。
发布于 2015-04-15 13:22:44
有一种简单的方法可以做到:
- (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;
你可以在上面的函数中检查一个标志,如果设置了标志,则返回零大小的值;如果没有设置,则返回正常值。在任何其他委托/数据源方法中都不需要更改。可以在其他位置设置/重置该标志。一旦标志改变,你需要调用:
hidden_flag = YES;
[self.collectionView reloadData];
不需要更改cellForItemAtIndexPath
。
发布于 2013-07-16 02:32:33
因为当您的单元格滚动到视图之外时,它会被回收。当滚动回到视图中时,集合视图(或表视图)将通过调用cellForItemAtIndexPath
重新创建它。因此,当为本应隐藏的单元格调用此方法时,您将不得不再次隐藏它。
有可能在从collectionView:cellForItemAtIndexPath:
返回单元格之后,框架正在对其调用[setHidden:NO]
。您可能需要继承UICollectionViewCell
的子类并提供自己的实现来完成此任务。或者向UICollectionViewCell
添加一个子视图以包含您的内容,然后根据需要隐藏/显示此子视图。
https://stackoverflow.com/questions/17661154
复制相似问题