为什么UICollectionView的UICollectionViewCell没有突出显示用户触摸?

时间:2013-01-31 23:26:44

标签: ios objective-c uicollectionview

我有一个由自定义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;
}

9 个答案:

答案 0 :(得分:33)

该类仅告诉您高亮状态,但不会更改视觉外观。您必须以编程方式执行此操作,例如:改变细胞的背景。

详情见CollectionView Programming Guide

答案 1 :(得分:32)

正如SAE所说,你必须在一个子类中自己做。我遇到的另一个问题是,当点击一个单元格时,它正在接收突出显示并重新绘制,如果按下并保持单元格。但是,如果快速拍摄,重绘永远不会发生。

我在故事板中创建了单元格,并且集合视图将“延迟内容触摸”标记为默认值。我解开了这个,它立刻显示出手指触摸屏幕。

我正在使用自定义绘制例程来检查isHighlighted值。您还需要在自定义单元格中覆盖setHighlighted,如下所示,或者永远不会调用绘图例程。

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

答案 2 :(得分:31)

您应该实施两种委托方法:

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

使用动画突出显示集合视图单元格的完整代码:

- (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 ];
}

答案 3 :(得分:8)

您需要实现 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;
 }

答案 4 :(得分:7)

您可以通过将这些行添加到UICellView的设置中来绘制一个hilight。

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

从“管理选择和亮点的视觉状态”... 集合视图默认支持单项选择,可以配置为支持多项选择或完全禁用选项。集合视图检测其边界内的点击并突出显示或相应地选择相应的单元格。在大多数情况下,集合视图仅修改单元格的属性以指示它已被选中或突出显示; 它不会改变细胞的视觉外观,但有一个例外。如果单元格的selectedBackgroundView属性包含有效视图,则集合视图会在突出显示或选择单元格时显示该视图。

答案 5 :(得分:3)

正如SAE指出的那样,您必须在突出显示的单元格中手动执行此操作。我找到的最简单方法是使用tableview didHighlightRowAtIndexPath和didUnhighlightRowAtIndexPath方法,这些方法在您的UICollectionCell实例中设置bool“highlight”,然后在子类UICollectionCell类中覆盖该属性。这样的美妙之处在于动画已经存在。您也可以在UITableView / UITableViewCell情况下执行相同操作。

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

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

答案 6 :(得分:1)

你可以试试这段代码:

- (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;
}

答案 7 :(得分:1)

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];
        }
    }

答案 8 :(得分:0)

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

- (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];
}