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

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

问题描述

根据集合视图编程指南应该处理 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 通过突出显示的属性独立管理其突出显示的状态。

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?

推荐答案

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

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天全站免登陆