2017-07-15 4 views
1

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メソッド呼び出しの後でのみ変更されます。私はなぜ同じコード部分に異なる応答をするのか分かりません。助けてくれてありがとう。

+0

は私を参照してください。この投稿への回答 - (リンク)[https://stackoverflow.com/questions/44393575/checkbox-uitableview-with-differ + 44398444#44398444] - UITableViewCellの 'UIButton'に2つのイメージを使用して、選択/非選択の" CheckBox "を作成します。これはあなたがしようとしているものに本当に近いはずです。 – DonMag

答えて

0

あなたは何をすべきかのボタンを伝える必要があり:

func buttonClicked(sender:UIButton) 
{ 
    if myButton.isSelected { 
     myButton.isSelected = false 
    }else{ 
     myButton.isSelected = true 
    } 
} 

はカスタムであるためにあなたのボタンを設定します。

let myButton = UIButton(type: .custom) 

または属性インスペクタで:

attributes Inspector forbutton

+0

が、私のバージョンではisSelectedにリネームされました。isSelected trueを設定すると、cellForRowAtメソッドの内部で期待通りに機能します。 – umutg4d

+0

質問:buttonClickedメソッドはまったくトリガーされますか? BTWがボタンの種類をカスタム –

+0

に設定しようとしました。はい、それがトリガーされます。また、私は、デバッグビューの階層を開くと、私はボタンの画像が正常に変更されたことがわかります。しかし、実際のデバイスのこの変更には反応しません。 – umutg4d

関連する問題