【发布时间】:2017-12-20 10:33:44
【问题描述】:
UITableViewCell 内的按钮不响应其 clicked 方法内的 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 方法后更改。我不明白为什么它对同一代码部分的响应不同。感谢您的帮助。
【问题讨论】:
-
查看我对这篇文章的回答 - (link)[stackoverflow.com/questions/44393575/… - 它使用两个图像作为
UIButton在UITableViewCell中创建一个选中/未选中的“复选框”,其中应该非常接近你想要做的事情。
标签: ios swift uitableview swift3 uibutton