gpt4 book ai didi

ios - 如何通过 uitableviewCell 中的单击操作更改 2 个 UIButton 图像

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:34 25 4
gpt4 key购买 nike

我在 tableview cell 上使用了 2 个 buttons 我得到了 sender 的操作,但是我需要在每次点击时更改按钮图像,例如:

加载中:

button1image = user.png
button2image = empty.png

如果用户点击按钮 1:

button1image = empty.png
button2image = user.png

如果用户点击按钮 2:

button1image = user.png
button2image = empty.png

在我的 tableviewcell 中:

  cell.button1.addTarget(self, action: #selector(ViewController.clickbutton1(_:)), forControlEvents: UIControlEvents.TouchUpInside)

cell.button2.addTarget(self, action: #selector(ViewController.clickbutton2(_:)), forControlEvents: UIControlEvents.TouchUpInside)

ViewController 中:

func clickbutton1(sender:UIButton) {

let buttonRow = sender.tag
}

func clickbutton2(sender:UIButton) {


let buttonRow = sender.tag

}

我还可以像这样更改图像中的按钮:

  sender.setImage(UIImage(named:"empty.png"), forState:UIControlState.Normal)

我该怎么做?

最佳答案

您可以这样尝试,创建包含图像名称的 2 个字符串实例,并像这样在 cellForRowAtIndexPath 中使用它。

var button1image = "user.png"
var button2image = "empty.png"

现在在 cellForRowAtIndexPath 中。

cell.button1.setImage(UIImage(named: button1image), forState: .Normal)
cell.button2.setImage(UIImage(named: button2image), forState: .Normal)

现在只需更改按钮操作中变量的值并重新加载 tableView。

func clickbutton1(sender:UIButton) {

let buttonRow = sender.tag
button1image = "empty.png"
button2image = "user.png"
self.tableView.reloadData()
}

func clickbutton2(sender:UIButton) {

let buttonRow = sender.tag
button1image = user.png
button2image = empty.png
self.tableView.reloadData()
}

关于ios - 如何通过 uitableviewCell 中的单击操作更改 2 个 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39332405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com