UITableViewCell 中的按钮不响应其点击方法中的 setImage 方法。这是 TableView 的 cellForRowAt 部分。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "BroadcastViewCell", for: indexPath) as!BroadcastViewCell
var show=self.week?[selectedIndex].shows?[indexPath.item]
cell.myLabel.text=show?.programName
if let resim=show?.thumbURL{
cell.myImage.downloadImage(from: resim)
}
cell.notifyButton.tag = indexPath.row
cell.notifyButton.setImage( UIImage(named:"icnAlarm"), for: .normal)
cell.notifyButton.setImage( UIImage(named:"icnAlarmCheck"), for: .selected)
if (self.notifications?.contains(where: {$0.identifier == (show?.airDate)!+(show?.airTime)!}))!
{
cell.notifyButton.isSelected=true
}else
{
cell.notifyButton.isSelected=false
}
cell.notifyButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
return cell;
}
我只是在 buttonClicked 实现中设置它的选中状态。
func buttonClicked(sender:UIButton)
{
sender.isSelected=true
}
但是图像在它之后没有改变。它仅在为其特定行调用 cellForRowAt 方法后发生变化。我不明白为什么它对同一代码部分的响应不同。感谢您的帮助。
最佳答案
你必须告诉按钮做什么:
func buttonClicked(sender:UIButton)
{
if myButton.isSelected {
myButton.isSelected = false
}else{
myButton.isSelected = true
}
}
将您的按钮配置为自定义:
let myButton = UIButton(type: .custom)
或者在属性检查器中:
关于ios - 单击后无法设置 UITableViewCell 中的 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117964/