2016-09-13 6 views
0

私は非常に困惑しています。私のcellForRowAtIndexPathメソッドでカスタムUITableViewCellでUIButtonにアクセスする

、私はUIButtonのタイトルを設定しようとしています:

が機能していない:

reactは私のUITableViewCellのサブクラスのサブビューでUIButton、ある
cell?.react?.setTitle("\(currentObject.likes)", forState: UIControlState.Normal) 

を。

UIButtonのタイトルを更新できないようです。私はそれが私のUITableViewCellのサブクラスでUITapGestureRecognizer経由で変更することが分かってきた唯一の方法、

ワーキング:

func reaction(button : UIButton) { 
    if button.imageView?.image == UIImage(named: "ic_favorite.png") { 
     button.setImage(UIImage(named: "ic_favorite_border.png"), forState: UIControlState.Normal) 
    } else { 
    button.setImage(UIImage(named: "ic_favorite.png"), forState: UIControlState.Normal) 
    } 
} 
+0

は 'react'であるあなたのテーブルのセルにそのボタンのタグを与え、このようにしてみてください?なぜあなたの反応はどこで私のカスタムセルのサブクラスであるのですか? – Tj3n

+0

あなたは反応のアクションを宣言した –

答えて

0

カスタムセル(たとえばmyCell)を作成し、@IBOutletとしてボタンを追加し、その後、入力このlet cell = tableView.dequeueReusableCellWithIdentifier("yourIdentifier", forIndexPath: indexPath) as! myCell

問題はあなたがボタンをセルにリンクしていないと思います&あなたが話しているボタンがXCodeに認識されません。

+0

ボタンの名前です! – scott

+0

これは理想的ではありませんが、うまくいきました。私はちょうどIBを介してそれをリンクし、それは働いた。 – scott

0

まず、メソッドに渡されたcellForRowAtIndexPathのセルへの参照を取得する必要があります。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier", forIndexPath: indexPath) as! UIButton   
    // do anything with cell 
    return cell 
} 

テーブルビューセル(UITableViewCell)の識別子を設定することを忘れないでください。

+0

古い投稿の編集について:古い投稿を編集しないでください。「あなたを歓迎しています。」[司会者は「感謝」と似たようなものを削除するだけの編集はお勧めしません] http://meta.stackoverflow.com/a/333096/3773011) " – Makyen

+0

公平ですね。指摘してくれてありがとう。 – navit

0

//まず、あなたのボタンがある

(cell?.contentView.viewWithTag(tag) as? UIButton)?.setTitle("\(currentObject.likes)", forState: UIControlState.Normal) 
関連する問題