import UIKit class ViewController: UIViewController,UITabBarDelegate,UITableViewDataSource { @IBOutlet weak var mytableView: UITableView! var Haru = ["cell 追加"] var newname = String() override func viewDidLoad() { super.viewDidLoad() } @IBAction func addbutton(_ 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.insert(textField.text!, at:0) //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) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Haru.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(); cell.textLabel?.text = kei[indexPath.row] cell.textLabel?.textColor = UIColor.brown cell.textLabel?.backgroundColor = UIColor.blue cell.textLabel?.font = UIFont.systemFont(ofSize: 30) return cell } func tableView(_ tableView: UITableView, didSelectRowAtIndexPath: IndexPath){ 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:0) //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) } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { kei.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) } } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
やりたいこと
他の人の質問を参考にしてcellをタップするとアラートテキストが表示され入力出来るようになったのですが理想としては[cell 追加]以外はタップしても何も入力できないようにしたいです。
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { switch indexPath.row { case 0: return indexPath // 選択不可にしたい場合は"nil"を返す case 1: return nil default: return indexPath } } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { if kei.count > (indexPath.row + 1) { return nil } return indexPath }
教えていただい通りにこのようにして見たのですがやっぱり[cell 追加]以外はタップしてもアラートテキストが出てくるのですがこの他にさらに別のメソッドが必要なでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/09/21 04:38
2017/09/21 04:52
退会済みユーザー
2017/09/21 05:25
2017/09/21 05:34
退会済みユーザー
2017/09/21 06:04
2017/09/21 06:16
退会済みユーザー
2017/09/21 12:14
2017/09/21 14:12
退会済みユーザー
2017/09/21 23:55
退会済みユーザー
2017/09/22 00:05
2017/09/22 01:20
退会済みユーザー
2017/09/23 12:16 編集
2017/09/23 17:55
退会済みユーザー
2017/09/23 23:58