UITableViewでチェックリストを作ってみたのですが、現在Cellを押すとチェックリストのUIImageが変更されます。Cellを押すと別のUITableViewに移動でき、UIImageを押下時にのみ画像が変更されるようにするにするにはどうすればいいでしょうか?
import
class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { let statusBarHeight = UIApplication.shared.statusBarFrame.height var checkListItem: [String : Bool] = [ "アイテム1" : false, "アイテム2" : false, ] let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // UITableView の作成 tableView.frame = CGRect( x: 0, y: statusBarHeight, width: self.view.frame.width, height: self.view.frame.height - statusBarHeight ) tableView.delegate = self tableView.dataSource = self self.view.addSubview(tableView) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var keys = [String](checkListItem.keys) keys.sort() let cellText = keys[indexPath.row] let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = cellText if self.checkListItem[cellText]! { cell.imageView?.image = UIImage(named: "checked") } else { cell.imageView?.image = UIImage(named: "unchecked") } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { let cellText = cell.textLabel?.text if cell.imageView?.image == UIImage(named: "checked") { self.checkListItem.updateValue(false, forKey: cellText!) cell.imageView?.image = UIImage(named: "unchecked") } else { self.checkListItem.updateValue(true, forKey: cellText!) cell.imageView?.image = UIImage(named: "checked") } cell.isSelected = false } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 56 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.checkListItem.count } }
タイトルと質問内容があっていないような気がするのですが?
まだ回答がついていません
会員登録して回答してみよう