之前是已经实现了:
给选中的cell添加背景色:
但是,对于选中之前,按下去的那一刻,是没有背景色的:
swift uitableviewcell before select add background color
ios – How to change color of UITableViewCell when selecting? – Stack Overflow
// Selection
// -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row.
// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
@available(iOS 6.0, *)
optional public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool
@available(iOS 6.0, *)
optional public func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath)
@available(iOS 6.0, *)
optional public func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath)
去添加didHighlightRowAtIndexPath,给cell设置背景色:
let CommonButtonColor:UIColor = UIColor(hexString: "#CC0486")!
func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
gLog.verbose("tableView=\(tableView), indexPath=\(indexPath)")
if let curCell = tableView.cellForRowAtIndexPath(indexPath) {
setSelectedColor(curCell)
}
}
func setSelectedColor(curCell:UITableViewCell) {
curCell.contentView.backgroundColor = CommonButtonColor
curCell.textLabel?.textColor = UIColor.whiteColor()
}
即可实现对应的效果: