Swift 更改表格视图单元格中的图像按钮
我有两个类Swift 更改表格视图单元格中的图像按钮,swift,uitableview,button,cell,Swift,Uitableview,Button,Cell,我有两个类TableViewController和CustomCell,然后将IB中的按钮(likeBtn)连接到CustomCell 现在,我想改变这个按钮的图像 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier = "Cell" let cell = tableView.d
TableViewController
和CustomCell
,然后将IB
中的按钮(likeBtn
)连接到CustomCell
现在,我想改变这个按钮的图像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
// Configure the cell...
cell.likeBtn.addTarget(self, action: #selector(TableViewController.liked), for: .touchUpInside)
return cell
}
@objc func liked(sender: UIButton) {
cell.likeBtn.setImage(UIImage(named: "liked.png"), for: .normal)
cell.likeBtn.isUserInteractionEnabled = false
}
但是在liked()
中,我有关于cell.likeBtn
的错误
我正在使用swift 4编程,正确的方法应该是:
@objc func liked(sender: UIButton) {
sender.setImage(UIImage(named: "liked.png"), for: .normal)
sender.isUserInteractionEnabled = false
}
您好,谢谢,我想到了
sender.tag
或pass参数,lol。现在我遇到了一个错误,当单击第一个单元格时,表中的一些单元格被单击!!如果您只想单击某些单元格按钮,只需在cellForRowAt中执行此操作:If indexath.row==(哪一行){cell.likeBtn.addTarget(self,action:#selector(TableViewController.like),for:.touchUpInside)}thx,但我想单击表中的任何单元格,并将其保存为我单击过的单元格!您可以始终添加标记,您的标记可以是indexath.row,这样每个按钮都有一个唯一的标记