Swift TableView防止按钮在选择时颜色变化

Swift TableView防止按钮在选择时颜色变化,swift,uitableview,uibutton,swift3,Swift,Uitableview,Uibutton,Swift3,当我在表格视图中选择一行时,我很难防止iAction按钮的背景色发生颜色变化。“我的按钮”的背景设置为绿色,但当我点击“表格视图”单元格时,它将变为灰色。我尝试通过不同的功能(tableViewDidSelectRowatineXpath等)更改颜色。有没有关于如何防止按钮背景发生变化的想法 提前谢谢 我在TableView中遇到了与按钮backgroundColor相同的问题,并通过这种方式解决了这个问题 func tableView(_ tableView: UITableView, did

当我在
表格视图中选择一行时,我很难防止
iAction
按钮的背景色发生颜色变化。“我的按钮”的背景设置为绿色,但当我点击“表格视图”单元格时,它将变为灰色。我尝试通过不同的功能(tableView
DidSelectRowatineXpath
等)更改颜色。有没有关于如何防止按钮背景发生变化的想法


提前谢谢

我在
TableView
中遇到了与按钮
backgroundColor
相同的问题,并通过这种方式解决了这个问题

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! CustomCell
    cell.button.backgroundColor = UIColor.greenColor()
}

我用上面Nirav提供的代码解决了这个问题:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! CustomCell
    cell.button.backgroundColor = UIColor.green
}
然而,我注意到,当我点击该行时,按钮背景颜色继续变为灰色,并按住“点击”按钮而不松开。我通过添加以下代码解决了这个问题:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as! CustomCell
    cell.exploreButton.backgroundColor = UIColor.green

}

如果要完全禁用选择时的颜色更改,请更改自定义单元格中的selectionStyle属性:

selectionStyle = .none

这个答案可能会引导您走向正确的方向:将tableview单元格选择样式更改为
None
?您好!谢谢你的回复。如果我只点击一个表视图单元格,代码就正常工作了。但是,如果我按住触摸键,按钮的背景色将变为灰色。你也有这个的解决方案吗?顺便说一句,很抱歉反应太晚。欢迎伴侣:)这应该是公认的答案-仅供参考,将其应用于单元格,而不是表格视图