質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

367閲覧

tableViewのスワイプについて

退会済みユーザー

退会済みユーザー

総合スコア0

TableView

TableView(UITableView)とは、リスト形式で表示するコントロールで、ほとんどのアプリに使用されています。画面を「行」に分けて管理し、一般的には各行をタップした際に詳細画面に移動します。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

1クリップ

投稿2017/09/20 13:52

編集2017/09/23 23:54
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```では出来たスワイプしての削除でさえ出来なくなりました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

UITableViewDelegate がないのでセルが反応しないように思いますが、スワイプして削除できたということは転記間違いでしょうか?
だとすると、他にも自身の環境と異なる箇所があるかもしれないですね。
また UITableViewDataSourceViewController と紐づいていないので、そもそもセルに何も表示されないと思いますが、これも転記間違いでしょうか。

クラス宣言と TableView の紐付けを追加して私の環境では動きました。

swift

1class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 2 3... 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 mytableView.delegate = self 9 mytableView.dataSource = self 10 }

さらに編集したいので、 editActionsForRowAt 中の以下の部分は、

swift

1self.kei.insert(textField.text!, at:0)

下記のように変更もしました。
上の状態だと編集しても新規に追加されます。

swift

1self.kei[indexPath.row] = textField.text!

投稿2017/09/24 14:43

ykws

総合スコア1236

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/09/24 16:08

viewDidLoadにdelegateについて書かないといけなかったんですね てっきりストーリーボードで結びつけたのでviewDidLoadには書かなくてもいいのかと思っていました。 それとclassのところUITableViewDelegateにしてたつもりだったんですが私の予測変換ミスでUITabBarDelegateになってたのを@ykwsさんのおかげで気づけました。夜遅く解答していただきありがとうございました。
退会済みユーザー

退会済みユーザー

2017/09/26 13:38

@ykwsさん [cell 追加 ボタン]の処理についてお伺いしたいことがあるのですがこのボタンの処理について分からなくて困っています。やりたいこととしては[cell 追加 ボタン]tableView上から消して他に作った[+]ボタンがタップされ時に初めてtabelView上に表示させたいのですがそのためにはどうしたらいいのでしょうか?下記に新しく立てた質問のURLがあるので見ていただきたいです。 [+]ボタンをタップするとアラートテキストが表示され入力した中身を題名としてcellに反映させることは出来たのですが、@IBAction func addbtr(_ sender: Any) { に中にどうindexPath.rowの内容を入れるかが分からないです。 お忙しと思いますが教えていただけないでしょうか? https://teratail.com/questions/93883
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問