import UIKit class ViewController: UIViewController, UITableViewDelegate/*UITabBarDelegate*/,UITableViewDataSource { @IBOutlet weak var mytableView: UITableView! var kei = [String]() override func viewDidLoad() { super.viewDidLoad() mytableView.delegate = self mytableView.dataSource = self } @IBAction func addbtr(_ sender: Any) { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { self.kei.append(textField.text!) 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 { case 1: return indexPath; default: return nil; } } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if kei.count > (indexPath.row + 1) { 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.count > (indexPath.row + 1) { // セルの選択不可にする cell.selectionStyle = UITableViewCellSelectionStyle.none cell.textLabel?.textColor = UIColor.black cell.backgroundColor = UIColor.yellow } else { // セルの選択を許可 cell.selectionStyle = UITableViewCellSelectionStyle.blue } return cell } func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) -> IndexPath? { if kei[indexPath.row] != "cell 追加 ボタン" { return [indexPath.row] }else /*if (self.kei.append("cell 追加 ボタン"))*/{ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { 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 } } 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) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in 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] } 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 追加ボタンと]と一緒に追加したい
困っていること
前までは+ボタンからでもアラートテキストで入力した内容がcellに追加できたのですが色々コードを書き足している内に出来なくなりました。
それと[cell 追加ボタン]がどういう仕組みでtableViewに表示されているのかが分からないので+ボタンの中に[cell 追加ボタン]の処理の書き方が分からなく困っています。
回答2件
あなたの回答
tips
プレビュー