import UIKit class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var mytableView: UITableView! var kei = [String]()//["cell 追加 ボタン"] override func viewDidLoad() { super.viewDidLoad() mytableView.delegate = self mytableView.dataSource = self // trueで複数選択、falseで単一選択 } @IBAction func addbtr(_ sender: Any) { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { self.kei.append(textField.text!) //insertは上に追加されappendは下に追加される self.kei.append("cell 追加 ボタン") } self.mytableView.reloadData() } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return kei.count } func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { switch indexPath.row { // 選択不可にしたい場合は"nil"を返す case 1: return indexPath; // 選択可にしたい場合は"indexPath"を返す default: return nil; } } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if kei[indexPath.row] != "cell 追加 ボタン" { return nil } return indexPath } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) //追加した内容に対して cell.textLabel?.text = kei[indexPath.row] cell.textLabel?.textColor = UIColor.red cell.backgroundColor = UIColor.black if kei[indexPath.row] != "cell 追加 ボタン" { cell.textLabel?.textColor = UIColor.black cell.backgroundColor = UIColor.yellow } if indexPath.row == 0 { cell.textLabel?.textColor = UIColor.black cell.backgroundColor = UIColor.white } return cell } /*if kei.count > (indexPath.row + 1)使うには func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ を func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? {*/ func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath) if cell?.textLabel?.text != "cell 追加 ボタン" { cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark tableView.deselectRow(at: indexPath, animated: true) return nil }else { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { self.kei.insert(textField.text!, at: indexPath.row)//indexPath.rowで1,2,3,4のような順になる //self.kei.insert(textField.text!, at:self.kei.count - 1) } self.mytableView.reloadData() } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) return indexPath } } //cellの削除について func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { self.kei.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) } } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .normal, title: "削除") {ation, index in self.kei.remove(at: (indexPath.row)) tableView.deleteRows(at: [indexPath], with: .fade) } delete.backgroundColor = .red let edit = UITableViewRowAction(style: .normal, title: "編集") { action, index in let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) // OKボタンの設定 let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in // OKを押した時入力されていたテキストを表示 if let textFields = alert.textFields { // アラートに含まれるすべてのテキストフィールドを調べる for textField in textFields { self.kei[indexPath.row] = textField.text! //self.kei.append(textField.text!) } self.mytableView.reloadData() } }) alert.addAction(okAction) // キャンセルボタンの設定 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) // テキストフィールドを追加 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() // シミュレータの種類によっては、これがないと警告が発生 // アラートを画面に表示 self.present(alert, animated: true, completion: nil) print("edit") } edit.backgroundColor = .orange return [delete,edit] } // trueを返すことでCellのアクションを許可しています func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { if kei.count > (indexPath.row + 1) { return true }else{ return false } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
やりたい事
[cell 追加 ボタン]とtitleのcell以外のcellをタップした時にチェックマークをつけたり、消したりできるようにしたいです
今出来ているところ
一応cellを追加する時にチェックマークが勝手につくように出来ました
困っているところ
cellが追加された時にはまだチェックマークがついてない状態にしたいです。
それとつけることは出来たのですがタップした時にチェックマークを消せない状態にいます。
elseで消す処理は書いているのですが反応してくれません。
それで下記のようにしてみればと考えたのですがcell追加時には確かにcellが追加されなくはなったのですがそしたら今度はcellにチェックマークがつかなくなりました
func didSelectRowAtIndexPathでこのようなコードを追加したところ[cell 追加 ボタン]だけcellが追加出来るようになりました
let cell = tableView.cellForRow(at: indexPath) if cell!.accessoryType == .checkmark{ cell!.accessoryType = .none }else{ cell!.accessoryType = .checkmark }
@IBAction func addbtr(_ sender: Any) { self.kei.append(textField.text!) self.kei.append("cell 追加 ボタン")
試したこと
if cell?.textLabel?.text != "cell 追加 ボタン" && cell.textLabel?.text != "textField.text!" {
@IBAction func addbtr
のなかのself.kei.append(textField.text!) をcell.textLabel?.text != "title"に当てはめればおそらく私のやりたい事が出来ると思うんですけどtextField.text!の場合""に入れても"cell 追加 ボタン"のようにチェックがつかないようにならないです。
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath) if indexPath.row == 0 { return nil } if cell?.textLabel?.text != "cell 追加 ボタン"{ cell?.accessoryType = cell?.accessoryType == .checkmark ? .none : .checkmark tableView.deselectRow(at: indexPath, animated: true) return nil
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。