为什么UICollectionView的UICollectionViewCell在用户触摸上不突出显示?

我有一个由自定义的UICollectionViewCell子类组成的UICollectionView。 单元格显示正确,并通过触发此方法正确响应用户的触摸:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

不过,我的理解是,当用户触摸单元格时,应该突出显示(蓝色),然后当用户抬起手指时突出显示应该消失。 这没有发生。 任何想法为什么?

这里是一些相关的代码:

在UICollectionView的数据源中:

 @implementation SplitCheckViewCollection - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"ReceiptCellIdentifier"; SplitCheckCollectionCell *cell = (SplitCheckCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; cell.cellName.text = [NSString stringWithFormat:@"%@%i",@"#",indexPath.row+1]; return cell; } 

在UICollectionViewCell的实现中:

 @implementation SplitCheckCollectionCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SplitCheckCollectionCell" owner:self options:nil]; if ([arrayOfViews count] < 1) { return nil; } if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; } self = [arrayOfViews objectAtIndex:0]; } return self; } 

class级只告诉你高亮状态,但不改变视觉外观。 你必须通过编程来改变单元格的背景。

CollectionView编程指南中介绍了详细信息。

正如SAE所说,你必须在一个子类中自己做。 我碰到的另一个障碍是,当点击一个单元格时,如果单元格被按下并保持,它正在接收高亮和重绘。 但是,如果快速点击,重绘从来没有发生过。

我已经在故事板中创build了单元格,并且collections夹视图默认为“延迟触摸内容”。 我解决了这个问题,它立即显示手指触摸屏幕。

我正在使用自定义绘制例程,检查isHighlighted值。 您还需要像下面那样覆盖自定义单元格中的setHighlighted,否则绘制例程将不会被调用。

 -(void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; [self setNeedsDisplay]; } 

有2个委托方法你应该实现:

 - (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath; - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath; 

突出显示具有animation的集合视图单元的完整代码:

 - (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; //set color with animation [UIView animateWithDuration:0.1 delay:0 options:(UIViewAnimationOptionAllowUserInteraction) animations:^{ [cell setBackgroundColor:[UIColor colorWithRed:232/255.0f green:232/255.0f blue:232/255.0f alpha:1]]; } completion:nil]; } - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; //set color with animation [UIView animateWithDuration:0.1 delay:0 options:(UIViewAnimationOptionAllowUserInteraction) animations:^{ [cell setBackgroundColor:[UIColor clearColor]]; } completion:nil ]; } 

如果要在触摸和释放触摸时使用“高亮”和“高亮”效果,则需要实现UICollectionViewDataSource

这里是示例代码

 #pragma mark - UICollectionView Datasource - (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor colorWithRed:235/255.0f green:236/255.0f blue:237/255.0f alpha:.5]; } - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = nil; } 

你可以通过将这些行添加到你的UICellView的设置来获得一个Hight的画图。

 UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds]; selectedBGView.backgroundColor = [UIColor redColor]; self.selectedBackgroundView = selectedBGView; 

从“pipe理可视化状态以进行select和突出显示”…集合视图默认情况下支持单项select,可configuration为支持多项select或完全禁用选项。 集合视图检测其边界内的分接点,并相应地突出显示或select相应的单元格。 大多数情况下,集合视图仅修改单元格的属性以指示它被选中或突出显示; 它不会改变你的细胞的视觉外观,只有一个例外。 如果单元格的selectedBackgroundView属性包含有效的视图,则集合视图将在单元格突出显示或选定时显示该视图。

正如SAE指出的那样,您将不得不在单元格中手动进行高亮显示。 我find的最简单的方法是使用tableview didHighlightRowAtIndexPath和didUnhighlightRowAtIndexPath方法,在UICollectionCell实例中设置bool“highlight”,然后覆盖子类UICollectionCell类中的该属性。 这就是animation已经在你身边了。 你也可以在UITableView / UITableViewCell的情况下做同样的事情。

所以在你的UICollectionView中使用UICollectionViewDelegate方法:

 func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) { collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None) } func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) { collectionView.deselectItemAtIndexPath(indexPath, animated: true) } 

然后在你的UICollectionViewCell子类中添加这个:

 override var highlighted:Bool{ didSet{ println("Highlighted is set \(highlighted)") if(highlighted == true){ self.backgroundColor = UIColor.redColor() }else{ self.backgroundColor = UIColor.blueColor() } } } 

如果要更改视觉效果,可以将单元格设置为在didHighlightItemAtIndexPath上select,并取消selectdidHighlightItemAtIndexPath,如下所示:

 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:NO]; } - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; } 

你可以试试这个代码:

 - (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor blueColor]; } 

 - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = nil; } 
 We can create our own highlight and unhighlight effect on collectionView cell by adding and removing a temporary view with some background color as follows: -(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *collectionViewCell = (UICollectionViewCell *) [collectionView cellForItemAtIndexPath:indexPath]; UIView *tapHighlightView = (UIView*)[collectionViewCell.contentView viewWithTag:10]; if (!tapHighlightView) { tapHighlightView = [[UIView alloc] initWithFrame:collectionViewCell.contentView.frame]; tapHighlightView.backgroundColor =[UIColor blackColor alpha:0.4]; tapHighlightView.tag = 10; [collectionViewCell.contentView addSubview:tapHighlightView]; } } -(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *collectionViewCell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; UIView *tapHighlightView = (UIView*)[collectionViewCell.contentView viewWithTag:10]; if (tapHighlightView != nil) { [tapHighlightView removeFromSuperview]; } }