objective-c UITableViewCell:如何防止蓝色选择​​背景w/o borking isSelected 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/899862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:35:33  来源:igfitidea点击:

UITableViewCell: How to prevent blue selection background w/o borking isSelected property?

iphoneobjective-ccocoa-touchuikit

提问by Todd Ditchendorf

I have a custom UITableViewCellsubclass. I have set the contentViewof my cell subclass to a custom UIViewclass in which i am overriding -drawRect:and doing all of the drawing there.

我有一个自定义UITableViewCell子类。我已经将contentView我的单元子类的 设置为一个自定义UIView类,我在其中覆盖-drawRect:并在那里完成所有绘图。

Also, I am setting cell.contentView.opaque = NOin order to achieve transparency in certain areas of the cell (unfortunately, a backgroud image behind the table must show thru each cell in certain parts to achieve a stylistic effect. i know this is a performance hit. it must be so).

此外,我设置cell.contentView.opaque = NO是为了在单元格的某些区域实现透明度(不幸的是,表格后面的背景图像必须通过某些部分的每个单元格显示以实现风格效果。我知道这是一个性能损失。它必须是所以)。

Problem: I still see the default pretty blue gradient background being drawn behind my cell (in the transparent areas) when it is selected or highlighted (being pressed). This is obscuring the image behind the table, which is bad.

问题:当我的单元格被选中或突出显示(被按下)时,我仍然看到默认的漂亮的蓝色渐变背景被绘制在我的单元格后面(在透明区域)。这会遮挡桌子后面的图像,这很糟糕。

Goal: To prevent the blue gradient background from appearing, but still be able to inspect the cell.isSelectedand cell.isHighlightedproperties from within -[MyContentView drawRect:]to determine how to draw my own custom selection/highlighting.

目标:防止出现蓝色渐变背景,但仍然能够从内部检查cell.isSelectedcell.isHighlighted属性-[MyContentView drawRect:]以确定如何绘制我自己的自定义选择/突出显示。

What I've tried:

我试过的:

  1. setting cell.selectionStyle = UITableViewCellSelectionStyleNonehas the desired effect of preventing the pretty blue gradient selection background, but also prevents the cell.isSelectedand cell.isHighlightedproperties from being properly set, which means i cannot do my own custom selection/highlight drawing

  2. setting cell.selectionBackgroundView = niland cell.backgroundView = nilin the cell's -initor -prepareForReusemethod does not prevent the blue gradient selection background

  3. setting cell.selectionBackgroundView = nilin the -[MyContentView -drawRect:]method doeshave the desired effect of preventing the blue gradient selection background, but that seems very janky

  4. overriding -[UITableViewCell setSelected:animated:] to be a no-op. this does not have the desired effect of preventing the blue gradient selection background

  1. 设置cell.selectionStyle = UITableViewCellSelectionStyleNone具有防止漂亮的蓝色渐变选择背景的预期效果,但也防止正确设置cell.isSelectedcell.isHighlighted属性,这意味着我无法进行自己的自定义选择/突出显示绘图

  2. 设置cell.selectionBackgroundView = nilcell.backgroundView = nil在单元格的-init-prepareForReuse方法中不会阻止蓝色渐变选择背景

  3. 设置cell.selectionBackgroundView = nil-[MyContentView -drawRect:]方法确实具有防止蓝色梯度选择背景的所希望的效果,但是,似乎非常janky

  4. 覆盖 -[UITableViewCell setSelected:animated:] 为空操作。这没有防止蓝色渐变选择背景的预期效果

回答by jessecurry

You must also override setHighlighted: to prevent the blue gradient from ever showing. If you just override setHighlighted: then you end up with a momentary selection effect.

您还必须覆盖 setHighlighted: 以防止显示蓝色渐变。如果你只是覆盖 setHighlighted: 那么你最终会得到一个瞬间的选择效果。

so you'll have these two methods:

所以你会有这两种方法:

- (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
{
    // don't highlight
}

- (void)setSelected: (BOOL)selected animated: (BOOL)animated 
{
    // don't select
    //[super setSelected:selected animated:animated];
}

回答by Mugunth

cell.selectionStyle = UITableViewCellSelectionStyleNone;

回答by Jason

An excellent resource on customizing UITableViews has been this postby Matt Gallagher. What you'll want to do is set the selectedBackgroundView to a new view (instead of nil) that is either transparent or a UIImageView.

关于自定义 UITableViews 的优秀资源是 Matt Gallagher 的这篇文章。您要做的是将 selectedBackgroundView 设置为透明或 UIImageView 的新视图(而不是 nil)。

回答by Max MacLeod

By far the easier method - in my opinion - to achieve this, is by setting the Table View attributes checkbox in the Interface Builder screen where it says "Show Selection on Touch". See the screenshot below:

到目前为止,在我看来,实现这一点的更简单的方法是在界面生成器屏幕中设置表视图属性复选框,其中显示“触摸时显示选择”。请看下面的截图:

enter image description here

在此处输入图片说明

回答by Max MacLeod

What has worked for me in the past is just putting:

过去对我有用的是:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { }

In my UITableViewCell subclasses (because it won't call super and make itself highlighted). Hope this is what you were looking for.

在我的 UITableViewCell 子类中(因为它不会调用 super 并突出显示自己)。希望这就是你要找的。

回答by Liam Goodacre

How about this?

这个怎么样?

// disable user interaction
cell.userInteractionEnabled = NO;