在哪里突出显示 UICollectionViewCell:委托还是单元格? [英] Where to highlight UICollectionViewCell: delegate or cell?

查看:17
本文介绍了在哪里突出显示 UICollectionViewCell:委托还是单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Collection View Programming Guide 应该在 UICollectionViewDelegate 中处理单元格高亮的视觉状态.像这样:

According to the Collection View Programming Guide one should handle the visual state of the cell highlights in the UICollectionViewDelegate. Like this:

- (void)collectionView:(PSUICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell highlight];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = (MYCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell unhighlight];
}

我不喜欢这种方法的地方在于,它向委托中添加了非常特定于单元格的逻辑.事实上,UICollectionViewCell 通过 highlighted 属性独立管理其高亮状态.

What I don't like about this approach is that it adds logic to the delegate that is very specific to the cell. In fact, UICollectionViewCell manages its highlighted state independently, via the highlighted property.

那么,重写 setHighlighted: 不是更干净的解决方案吗?

Wouldn't overriding setHighlighted: be a cleaner solution, then?

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    if (highlighted) {
        [self highlight];
    } else {
        [self unhighlight];
    }
}

这种方法比委托方法有什么缺点吗?

Are there any disadvantages to this approach instead of the delegate approach?

推荐答案

正如文档所说,您可以在单元格突出显示时依赖 highlighted 属性进行更改.例如,以下代码将在突出显示时使单元格变为红色(但不是它的子视图):

As the documentation says, you can rely on highlighted property to be changed while the cell is highlighted. For example the following code will make the cell red when highlighted (not its subviews though):

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

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    if (self.highlighted) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(context, 1, 0, 0, 1);
        CGContextFillRect(context, self.bounds);
    } 
}

如果你添加这样的东西,背景会变成紫色(红色+不透明的蓝色):

And if you add something like this the background will become purple (red + opaque blue):

- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
}

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

因此您可以同时使用两者(不一定同时更改单元格外观).不同之处在于,使用委托方法您还拥有 indexPath.它可用于创建多选(您将与选择委托方法一起使用此方法),在单元格突出显示时显示一些预览,在其他视图中显示一些动画......这个委托有很多设备我认为的方法.

So you can use both together (not necessarily both changing the cell appearance). The difference is that with delegate methods you also have indexPath. It might be used to create multi-selection (you will use this methods together with selection delegate methods), to show some preview while the cell is highlighted, to show some animation with other views... There's quite a few appliance for this delegate methods in my opinion.

作为结论,我会将单元格外观留给单元格本身处理,并使用委托方法让控制器同时制作一些很酷的东西.

As a conclusion, I would leave the cell appearance to be handled by the cell itself and use delegate methods to let controller make something cool in the same time.

这篇关于在哪里突出显示 UICollectionViewCell:委托还是单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆