ios 自定义 UITableViewCell 选择样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11920156/
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
Custom UITableViewCell selection style?
提问by SimplyKiwi
When I click on my UITableViewCell
, the background part (the areas that my background image doesn't cover) turns blue when I click on the cell. Also, all of the UILabel
s on the cell turn to white when it is clicked which is what I want.
当我单击 my 时UITableViewCell
,当我单击单元格时,背景部分(我的背景图像未覆盖的区域)变为蓝色。此外,UILabel
单击单元格上的所有s 都会变成白色,这正是我想要的。
However what I do not want is the blue background when I click it but if I do selectionstylenone
, then I lose the highlighted colours for the UILabel
s in the cell.
但是,当我单击它时,我不想要的是蓝色背景,但是如果我这样做了selectionstylenone
,那么我将丢失UILabel
单元格中 s的突出显示颜色。
So is there any way to just get rid of the blue background when the cell is clicked but to keep the highlighted colors of the UILabel
s?
那么有什么方法可以在单击单元格时摆脱蓝色背景,但保留UILabel
s的突出显示颜色?
回答by jonkroll
You can do this as follows. Set your table cell's selection style to UITableViewCellSelectionStyleNone
. This will remove the blue background highlighting. Then, to make the text label highlighting work the way you want, instead of using the default UITableViewCell class, create a subclass of UITableViewCell
and override the default implementation of setHighlighted:animated
with your own implementation that sets the label colors to however you want depending on the highlighted state.
您可以按如下方式执行此操作。将表格单元格的选择样式设置为UITableViewCellSelectionStyleNone
。这将删除蓝色背景突出显示。然后,为了使文本标签以您想要的方式突出显示,而不是使用默认的 UITableViewCell 类,创建一个子类UITableViewCell
并setHighlighted:animated
使用您自己的实现覆盖默认实现,根据突出显示状态将标签颜色设置为您想要的.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}
回答by Alfa
If working before iOS7, make your cell selection style none
如果在 iOS7 之前工作,请将您的单元格选择样式设置为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Else, leave it by UITableViewCellSelectionStyleDefault
否则,留下它 UITableViewCellSelectionStyleDefault
Then:
然后:
UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedView;
This code will work properly
此代码将正常工作
回答by Scar
You can use the following delegate methods after you set the selection style to none:
将选择样式设置为无后,您可以使用以下委托方法:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
Implement your code here, like this
在这里实现你的代码,就像这样
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell.lbls setTextColor:[UIColor whiteColor]];
return indexPath;
}
回答by Mrunal
In cellForRowAtIndexPath use this code:
在 cellForRowAtIndexPath 中使用以下代码:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels
Hope this will work for you.
希望这对你有用。
Enjoy Coding :)
享受编码:)
回答by who9vy
To get this work you have to set the selection style to UITableViewCellSelectionStyleNone
and then you should override the method setSelected:animated:
to get the result you want. It does the same thing the automated selection mechanism of iOS does when you see the blue (or gray) selection.
要完成这项工作,您必须将选择样式设置为UITableViewCellSelectionStyleNone
,然后您应该覆盖该方法setSelected:animated:
以获得您想要的结果。当您看到蓝色(或灰色)选择时,它的作用与 iOS 的自动选择机制相同。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}
You can also customize this in another way, e.g. by changing the UITableViewCell background, etc.
您还可以通过另一种方式自定义它,例如通过更改 UITableViewCell 背景等。
回答by superarts.org
Overriding the following functions in your subclass of UITableViewCell
.
覆盖UITableViewCell
.
override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }
回答by Logan Gauthier
In order to match the standard selection style behavior, you'll want to override both setHighlighted:animated:
and setSelected:animated:
. You'll probably want to move that code into a shared method in order to avoid duplicate code.
为了匹配标准的选择样式行为,您需要覆盖setHighlighted:animated:
和setSelected:animated:
。您可能希望将该代码移动到共享方法中以避免重复代码。
override func setHighlighted(highlighted: Bool, animated: Bool) {
setAsSelectedOrHighlighted(highlighted, animated: animated)
super.setHighlighted(highlighted, animated: animated)
}
override func setSelected(selected: Bool, animated: Bool) {
setAsSelectedOrHighlighted(selected, animated: animated)
super.setSelected(selected, animated: animated)
}
func setAsSelectedOrHighlighted(selectedOrHighlighted: Bool, animated: Bool) {
let action = {
// Set animatable properties
}
if animated {
UIView.animateWithDuration(1.0, delay: 0, options: .CurveEaseInOut, animations: action, completion: nil)
}
else {
action()
}
}
回答by zeeawan
In your custom cell, override the default implementation of awakeFromNib & setSelected:
在您的自定义单元格中,覆盖awakeFromNib 和setSelected 的默认实现:
- (void)awakeFromNib {
// Initialization code
UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.image = [UIImage imageNamed:@"cell_selected_img"];
self.selectedBackgroundView = imageView;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
self.lblCustomText.textColor = [UIColor whiteColor];
} else {
self.lblCustomText.textColor = [UIColor blackColor];
}
}
Also make sure that the selection styleis NOTset to None.
另外,还要确保选择的风格是不设置为无。
回答by Fabricio PH
The only way I could get it working was by:
我可以让它工作的唯一方法是:
- (void)awakeFromNib {
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(55.0/255.0) green:(163.0/255.0) blue:(237.0/255.0) alpha:1.0];
bgColorView.layer.masksToBounds = YES;
self.selectedBackgroundView = bgColorView;
}
回答by Alex Kolovatov
Also you can use contentView.alpha. Here is the example.
您也可以使用 contentView.alpha。这是示例。
First, set selection style for your cell:
首先,为您的单元格设置选择样式:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Next, in custom cell class override this method with animation example:
接下来,在自定义单元类中使用动画示例覆盖此方法:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[UIView animateWithDuration:0.15f animations:^{
self.contentView.alpha = 0.5f;
}];
} else {
[UIView animateWithDuration:0.35f animations:^{
self.contentView.alpha = 1.f;
}];
}
}