2017-04-21 115 views
2

我想突出顯示我的tableView中的一些單元格。突出顯示我的意思是將單元格的背景顏色設置爲例如複製文本時使用的藍色。我用嘗試這種代碼,以檢查是否正常工作突出顯示一個tableViewCell

public func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     cell.setHighlighted(true, animated: false) 
    } 

我也試過這個

cell.setSelected(true, animated: false) 

在這兩種情況下,細胞變得灰色和藍色沒有。有沒有解決方案?

+0

你試圖設置單元格的'selectionStyle'到'blue'? –

回答

4

更改selectionStyle yourcell的屬性。如果您想將其更改爲UITableViewCellSelectionStyleBlue,則它將是藍色。對於另一種方法,你可以得到另一個SO answer

爲如

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
cell.selectionStyle = .blue 
} 

UITableViewCell有三種默認的選擇樣式: -

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) { 
UITableViewCellSelectionStyleNone, 
UITableViewCellSelectionStyleBlue, 
UITableViewCellSelectionStyleGray, 
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0) 
}; 

,或者你可以直接設置Selection color在你的細胞類似

enter image description here

0

嘗試這樣

public func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     cell.selectionStyle = .blue 
    }