2011-08-19 53 views
0

我有我自己的子類UITableViewCell,我想在用戶按下單元格時進行自定義。在自定義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:,並且您看不到任何上述代碼生效。

有沒有人知道我在做什麼錯在這裏?

回答

1

嗯,你可以處理單元格被選中而不是突出顯示的情況。例如:

- (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]]; 
     } 
    }