Cocoa touch 自定义UITableViewCell上的奇怪高亮显示

Cocoa touch 自定义UITableViewCell上的奇怪高亮显示,cocoa-touch,uitableview,Cocoa Touch,Uitableview,我有自己的UITableViewCell子类,当用户按下单元格时,我想自定义该子类 因此,我尝试重写以下方法: - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { if (highlighted) { [self setBackgroundColor:[UIColor orangeColor]]; [self.optionLabel setTextColor:[UIC

我有自己的
UITableViewCell
子类,当用户按下单元格时,我想自定义该子类

因此,我尝试重写以下方法:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted)
    {
        [self setBackgroundColor:[UIColor orangeColor]];
        [self.optionLabel setTextColor:[UIColor whiteColor]];
    }
    else
    {
        [self setBackgroundColor:[UIColor clearColor]];
        [self.optionLabel setTextColor:[UIColor orangeColor]];
    }

    [super setHighlighted:highlighted animated:animated];
}
如果我按住手机,效果会很好。但是如果我快速点击单元格,我的
UITableView
会捕捉到代理回调
tableView:didSelectRowAtIndexPath:
,您无法看到我上面的任何代码生效


有人知道我做错了什么吗?

好吧,当单元格被选中而不是突出显示时,您可以处理这种情况。比如:

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

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
        [self updateCell];
    }

    - (void)updateCell {
        if (self.highlighted || self.selected) {
            [self setBackgroundColor:[UIColor orangeColor]];
            [self.optionLabel setTextColor:[UIColor whiteColor]];
        } else {
            [self setBackgroundColor:[UIColor clearColor]];
            [self.optionLabel setTextColor:[UIColor orangeColor]];
        }
    }