UITableViewでチェックリストを作ってみたのですが、現在Cellを押すとチェックリストのUIImageが変更されます。Cellを押すと別のUITableViewに移動でき、UIImageを押下時にのみ画像が変更されるようにするにするにはどうすればいいでしょうか?
import
1 2class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 3 4 let statusBarHeight = UIApplication.shared.statusBarFrame.height 5 6 var checkListItem: [String : Bool] = [ 7 "アイテム1" : false, 8 "アイテム2" : false, 9 ] 10 11 let tableView = UITableView() 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 // UITableView の作成 17 tableView.frame = CGRect( 18 x: 0, 19 y: statusBarHeight, 20 width: self.view.frame.width, 21 height: self.view.frame.height - statusBarHeight 22 ) 23 tableView.delegate = self 24 tableView.dataSource = self 25 self.view.addSubview(tableView) 26 } 27 28 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 29 30 var keys = [String](checkListItem.keys) 31 32 keys.sort() 33 34 let cellText = keys[indexPath.row] 35 36 let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 37 cell.textLabel?.text = cellText 38 39 if self.checkListItem[cellText]! { 40 cell.imageView?.image = UIImage(named: "checked") 41 } else { 42 cell.imageView?.image = UIImage(named: "unchecked") 43 } 44 45 return cell 46 } 47 48 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 49 50 if let cell = tableView.cellForRow(at: indexPath) { 51 52 let cellText = cell.textLabel?.text 53 54 if cell.imageView?.image == UIImage(named: "checked") { 55 56 self.checkListItem.updateValue(false, forKey: cellText!) 57 cell.imageView?.image = UIImage(named: "unchecked") 58 } else { 59 60 self.checkListItem.updateValue(true, forKey: cellText!) 61 cell.imageView?.image = UIImage(named: "checked") 62 } 63 64 cell.isSelected = false 65 } 66 } 67 68 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 69 return 56 70 } 71 72 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 73 return self.checkListItem.count 74 } 75}
タイトルと質問内容があっていないような気がするのですが?
回答4件
あなたの回答
tips
プレビュー