UITableViewCell内のボタンは、クリックされたメソッド内でsetImageメソッドに応答しません。 TableViewのcellForRowAtセクションを次に示します。TableViewCell内のUIButtonをクリックした後にイメージを設定できませんでした
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メソッド呼び出しの後でのみ変更されます。私はなぜ同じコード部分に異なる応答をするのか分かりません。助けてくれてありがとう。
は私を参照してください。この投稿への回答 - (リンク)[https://stackoverflow.com/questions/44393575/checkbox-uitableview-with-differ + 44398444#44398444] - UITableViewCellの 'UIButton'に2つのイメージを使用して、選択/非選択の" CheckBox "を作成します。これはあなたがしようとしているものに本当に近いはずです。 – DonMag