2017-05-19 16 views
0

この関数は、各ステータスをUITableViewCellに表示します。イメージのステータスは緑色(デフォルト)です。ボタンの1つをクリックすると、赤色の画像が表示されます。SwiftのTableViewCellでボタンをクリックして画像を表示するには?

クリックボタン機能は

func get(_ sender: UIButton){} 

である。これは、それが動作しない表示赤画像

let imageName = "red.png" 
let image = UIImage(named: imageName) 
index.red.image = image 

です!の解き方?コードは、私はそれが赤いを表示するボタンのいずれかをクリックするとこれは、ディスプレイのデフォルトある

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for:indexPath) as! TableViewCell 
    cell.gets.tag = indexPath.row 
    cell.gets.addTarget(self, action: #selector(self.get), for: .touchDown) 


     let statusLabel = cell.viewWithTag(3) as! UILabel 
     statusLabel.text = String(posts[indexPath.row].status) 

    return cell 

} 

func get(_ sender: UIButton){ 
    let databaseRef = FIRDatabase.database().reference() 

    let index = sender.tag 
    databaseRef.child("location").queryOrderedByKey().observe(.childAdded, with: {snapshot in 
     guard let firebaseResponse = snapshot.value as? [String:Any] else 
     { 
      print("Snapshot is nil hence no data returned") 
      return 
     } 
     let key = snapshot.key 

    let updates: [String: Any] = [ 
     "/id": key, 
     "status": "get it" 
    ] 
    databaseRef.child("location").child(self.posts[index].key).updateChildValues(updates) 


}) 
    /* 
    let imageName = "red.png" 
    let image = UIImage(named: imageName) 
    index.red.image = image 
    */ 

} 

Click this image です。

+0

を変更することができ、サンプルコードであることに注意してくださいチェックボタンをクリックしたときに、そのイメージチェンジをしたいですか? – KKRocks

答えて

0

存在するセルに変更が必要です。

次のようにそれはあなたがあなたのニーズに従って

func get(_ sender: UIButton){ 
     let cell: TableViewCell? = (sender.superview?.superview as? TableViewCell) // track your cell here 
     var indexPath: IndexPath? = yourtbleView?.indexPath(for: cell!) 
     cell.red.image = image // change with cell image view 
     } 
+0

本当にありがとう! – Peter

関連する問題