2017-08-13 429 views
0

我有一個自定義UITableViewCell子類,我想顯示突出顯示狀態,但沒有選擇狀態。突出顯示UITableView沒有選擇

如何突出顯示UITableViewCell觸摸背景(並且釋放時不高亮),但不顯示選擇狀態單元背景(我有自己的自定義選擇狀態)?

回答

0
@implementation CustomTableViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    // Initialization code 
} 

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

    // You have your code here as you said 
} 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated 
{ 
    //Implement custom hilighting code 
} 

@end 
0

您需要實現UITableView delegate方法

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 
} 
+0

這實際上取消though-我只是想自定義狀態的細胞。 – arooo

0

首先,你需要做的表視圖不可選擇通過設置allowsSelectionfalse

接下來,創建這個定製表格視圖單元格:

class MyCell: UITableViewCell { 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     backgroundColor = .red // or some other color 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     backgroundColor = .white 

    } 
} 

使用表格視圖此自定義單元格。你可以做這樣的事情在cellForRowAtIndexPath

let cell = MyCell() 
cell.isUserInteractionEnabled = true 
// configure the cell's other stuff 
return cell