前提・実現したいこと
ここに質問の内容を詳しく書いてください。
(例)xcode 6.1で初めてアプリを作成しています。
カスタムセル一行の左端にチェックボックス(画像)があり、その右にラベルがあるTableViewを作成しようとしています。
発生している問題・エラーメッセージ
左端のチェックボックス(画像)をクリックした場合、チェックON/OFF画像の表示切り替えをしたいのですが、うまくできません。Print分のデバッグで反応する事は確認できてはいます。ご教示よろしくお願いいたします。
カスタムセルのソースコード
class CustomCellTableViewCell: UITableViewCell {
@IBOutlet weak var checkImage: UIImageView! @IBOutlet weak var label: UILabel! @IBOutlet weak var button: UIButton! var check = false let checkOn:UIImage = UIImage(named:"checkOn.jpg")! let checkOff:UIImage = UIImage(named:"checkOff.jpg")! @IBAction func checkButton(sender: AnyObject) { if(!check){ sender.setImage(checkOn, forState: UIControlState.Normal) //sender.setTitle("on", forState: UIControlState.Normal) println("checkOn") println("label: (self.label.text)") println("checkImage: (self.checkImage)") println("self: (self)") check = !check } else{ sender.setImage(checkOff, forState: UIControlState.Normal) println("checkOff") check = !check } } func setCellData(indexPath: NSIndexPath){ let cell = self as UITableViewCell self.label.text = String((indexPath.row)+1)
// self.checkImage.image = UIImage(named: "checkOn.jpg")
// self.button.setImage(checkOn, forState: UIControlState.Normal)
}
}
ViewControllerのソースコード
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView! override func viewDidLoad() { tableView.registerNib(UINib(nibName: "CustomCellTableViewCell", bundle: nil),forCellReuseIdentifier:"reuseCell") } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellValue = tableView.dequeueReusableCellWithIdentifier("reuseCell", forIndexPath: indexPath) as CustomCellTableViewCell cellValue.setCellData(indexPath)
// println("cellValue: (cellValue.textField)")
//gestureを設定 let tapGesture = UITapGestureRecognizer(target: self, action: Selector("tapIcon:")) cellValue.checkImage.addGestureRecognizer(tapGesture) cellValue.checkImage.userInteractionEnabled = true return cellValue } func tapIcon(gestureRecognizer: UITapGestureRecognizer) { let tappedLocation = gestureRecognizer.locationInView(tableView) let tappedIndexPath = tableView.indexPathForRowAtPoint(tappedLocation) let tappedCell = tableView.cellForRowAtIndexPath(tappedIndexPath!)! println("tappedCell: (tappedCell)")
// println("tappedCelllabel: (tappedCell.label.text)")
// println("tappedCell: (tappedCell.checkImage)")
// tappedCell.image.setImage("checkOn.jpg", forState: UIControlState.Normal)
// println("tappedCell.label: (tappedCell.label.text)")
} override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}