gpt4 book ai didi

ios - 如何单击其中一个按钮在 Swift 的 TableViewCell 中显示图像?

转载 作者:行者123 更新时间:2023-11-30 12:26:57 26 4
gpt4 key购买 nike

该函数显示UITableViewCell中的各个状态。图像的状态为绿色(默认)。当我单击其中一个按钮时,它将显示红色图像。

点击按钮的功能是

func get(_ sender: UIButton){} 

这是显示红色图像

let imageName = "red.png"
let image = UIImage(named: imageName)
index.red.image = image

这不起作用!怎么解决?代码是

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for:indexPath) as! TableViewCell
cell.gets.tag = indexPath.row
cell.gets.addTarget(self, action: #selector(self.get), for: .touchDown)


let statusLabel = cell.viewWithTag(3) as! UILabel
statusLabel.text = String(posts[indexPath.row].status)

return cell

}

func get(_ sender: UIButton){
let databaseRef = FIRDatabase.database().reference()

let index = sender.tag
databaseRef.child("location").queryOrderedByKey().observe(.childAdded, with: {snapshot in
guard let firebaseResponse = snapshot.value as? [String:Any] else
{
print("Snapshot is nil hence no data returned")
return
}
let key = snapshot.key

let updates: [String: Any] = [
"/id": key,
"status": "get it"
]
databaseRef.child("location").child(self.posts[index].key).updateChildValues(updates)


})
/*
let imageName = "red.png"
let image = UIImage(named: imageName)
index.red.image = image
*/

}

Click this image这是默认显示绿色,当我单击其中一个按钮时,它将显示红色

最佳答案

您需要更改现有单元格。如下

请注意,这是您可以根据需要修改的示例代码

   func get(_ sender: UIButton){
let cell: TableViewCell? = (sender.superview?.superview as? TableViewCell) // track your cell here
var indexPath: IndexPath? = yourtbleView?.indexPath(for: cell!)
cell.red.image = image // change with cell image view
}

关于ios - 如何单击其中一个按钮在 Swift 的 TableViewCell 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063853/

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