首页 > 解决方案 > 如何在 UITableViewCell 上突出显示 UIButton

问题描述

单击时如何设置UIButton突出显示请帮助我,因为我完全陷入了这段代码中

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
    let cell = self.leaveDetailTableView.cellForRow(at: indexPath) as? LeaveDetailCell
    cell!.cellCardView.backgroundColor = #colorLiteral(red: 0.9568627451, green: 0.8941176471, blue: 0.6549019608, alpha: 1)
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
    let cell = self.leaveDetailTableView.cellForRow(at: indexPath) as? LeaveDetailCell
    cell!.cellCardView.backgroundColor = UIColor.white
}

当我选择一个表格视图项目时,我必须用我想选择我自己的颜色的颜色突出显示一个特定的行。

标签: iosswiftuitableviewuicolor

解决方案


如果您想在单击按钮时更改颜色,您可以这样做首先,您应该向按钮添加一个操作

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
cell.cellCardView.addGestureRecognizer(tapGesture)

完成此操作后,在操作handleTapGesture(_:)中,您可以像这样更改按钮的颜色

func handleTapGesture(_ sender: UIButton) {
    UIView.animate(withDuration: 0.1, animations: {
        sender.backgroundColor = #colorLiteral(red: 0.9568627451, green: 0.8941176471, blue: 0.6549019608, alpha: 1)
    }) { (_) in
        sender.backgroundColor = .white
    }
}

推荐阅读