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. } }
分からないことと解決したいこと
ネットで調べとことを参考にしてtableViewをスワイプして削除させることはできたのですが、それに加えてスワイプしたときに編集ボタンを追加し、編集ボタンをタップした際にアラートテキストを出しデータの編集をしたいのですがどのようにすればいいか分からなくて困っています。
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 more = UITableViewRowAction(style: .normal, title: "削除") { action, index in self.kei.remove(at: (indexPath.row)) tableView.deleteRows(at: [indexPath], with: .fade) print("more button tapped") } more.backgroundColor = .lightGray let favorite = 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.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) print("favorite button tapped") } favorite.backgroundColor = .orange return [favorite, more] }
出来ないこと
func editingStyle```を使うとスワイプして削除はできるのですが、スワイプした時に削除ボタンの他に編集ボタンを加えようと```func editingStyle```のコードを消して```func editActionsForRowAt```に変えてコードを書いて見たのですがそうすると```func editingStyle```では出来たスワイプしての削除でさえ出来なくなりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/09/24 16:08
退会済みユーザー
2017/09/26 13:38