可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
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.
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?
回答1:
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.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted) {
self.textLabel.textColor = [UIColor whiteColor];
} else {
self.textLabel.textColor = [UIColor blackColor];
}
}
回答2:
If working before iOS7, make your cell selection style none
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Else, leave it by UITableViewCellSelectionStyleDefault
Then:
UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedView;
This code will work properly
回答3:
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;
}
回答4:
In cellForRowAtIndexPath use this code:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels
Hope this will work for you.
Enjoy Coding :)
回答5:
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.
- (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.
回答6:
Overriding the following functions in your subclass of UITableViewCell
.
override func setHighlighted(highlighted: Bool, animated: Bool) { }
override func setSelected(selected: Bool, animated: Bool) { }
回答7:
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.
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()
}
}
回答8:
In your custom cell, override the default implementation of 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 style is NOT set to None.
回答9:
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;
}
回答10:
Also you can use contentView.alpha. Here is the example.
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;
}];
}
}