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

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

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

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

Xcode

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

2262閲覧

delete機能について

blakekei

総合スコア35

iOS

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

Xcode

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/01/06 14:08

swift

1コード 2import UIKit 3 4class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 7 @IBOutlet weak var tableView: UITableView! 8 9 var sectionTitleArray = [String]() 10 11 var dataArrayGroup: [[String]] = [] 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tableView.estimatedRowHeight = 44 17 tableView.rowHeight = UITableViewAutomaticDimension 18 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 19 } 20 21 // Sectionを追加する(アニメーションはご自由に) 22 @IBAction func addSection(_ sender: Any) { 23 //func addSection() { 24 25 let alert = UIAlertController(title:"タイトル", 26 message: "メッセージ", 27 preferredStyle: .alert) 28 29 let cancelAction = UIAlertAction(title: "Cancel", 30 style: .cancel, 31 handler: 32 { action -> Void in 33 print("Cancel") 34 }) 35 36 let defaultAction = UIAlertAction(title: "OK", 37 style: .default, 38 handler: 39 { action -> Void in 40 41 // TextFieldから値を取得 42 if let textFields = alert.textFields { 43 for textField in textFields { 44 45 if let text = textField.text, !text.isEmpty { 46 47 // 取得したテキストをセクションのタイトルとして追加する 48 print(text) 49 50 self.sectionTitleArray.insert(text, at: 0) 51 self.dataArrayGroup.insert([], at: 0) 52 self.tableView.insertSections(IndexSet(integer: 0), with: .automatic) 53 } 54 } 55 } 56 }) 57 58 alert.addAction(cancelAction) 59 alert.addAction(defaultAction) 60 61 alert.addTextField(configurationHandler: { text -> Void in 62 63 }) 64 65 present(alert, animated: true, completion: nil) 66 67 } 68 69 70 // Rowを追加する(アニメーションはご自由に) 71 @IBAction func addRow(_ sender: Any) { 72 //func addRow() { 73 74 if dataArrayGroup.count == 0 { 75 return 76 } 77 78 let count = dataArrayGroup[0].count 79 let alert = UIAlertController(title:"タイトル", 80 message: "メッセージ", 81 preferredStyle: .alert) 82 83 let cancelAction = UIAlertAction(title: "Cancel", 84 style: .cancel, 85 handler: 86 { action -> Void in 87 print("Cancel") 88 }) 89 90 let defaultAction = UIAlertAction(title: "OK", 91 style: .default, 92 handler: 93 { action -> Void in 94 95 // TextFieldから値を取得 96 if let textFields = alert.textFields { 97 for textField in textFields { 98 99 if let text = textField.text, !text.isEmpty { 100 101 // 取得したテキストをセクションのタイトルとして追加する 102 print(text) 103 104 105 self.dataArrayGroup[0].insert(String(count), at: count) 106 self.tableView.insertRows(at: [IndexPath(row: count, section: 0)], with: .automatic) 107 } 108 } 109 } 110 }) 111 112 alert.addAction(cancelAction) 113 alert.addAction(defaultAction) 114 115 alert.addTextField(configurationHandler: { text -> Void in 116 117 }) 118 119 present(alert, animated: true, completion: nil) 120 } 121 122 123 // MARK: - TableView Delegate & DataSource 124 //この部分です。 125 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 126 if sectionTitleArray.count == 0 { 127 return nil 128 } else { 129 return sectionTitleArray[section] 130 } 131 } 132 133 // Section Count 134 func numberOfSections(in tableView: UITableView) -> Int { 135 return dataArrayGroup.count 136 } 137 138 // Row Count 139 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 140 return dataArrayGroup[section].count 141 } 142 143 // Generate Cell 144 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 145 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 146 let dataArray = dataArrayGroup[indexPath.section] 147 cell.textLabel?.text = dataArray[indexPath.row] 148 return cell 149 } 150 151 // Select Cell 152 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 153 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 154 } 155 156 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 157 if editingStyle == UITableViewCellEditingStyle.delete { 158 dataArrayGroup.remove(at: indexPath.row) 159 tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade) 160 } 161 } 162 163 //cellが削除が可能なことを伝える 164 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 165 return UITableViewCellEditingStyle.delete; 166 } 167}

cellをdeleteのdeleteできるようにしたくdeleteの機能を追加したのですがエラーが起きてしまい自分なりに色々考えましたがよくわからなかったのでどこのコードが間違っているのかを知りたく質問させていただきました。
それとできればsectionもdelete出来るようにしたいので教えていただけたら嬉しいです

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

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

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

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

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

guest

回答2

0

ベストアンサー

rowの削除に関しては以下の部分のみ書き換える事でできますよ。

swift

1func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 2 if editingStyle == UITableViewCellEditingStyle.delete { 3 dataArrayGroup[indexPath.section].remove(at: indexPath.row) 4 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 5 } 6}

sectionの削除

ボタンを何処かに追加して以下のメソッドとひも付けてください。
※とりあえず決め打ちで0番目のsectionを削除しています。

swift

1@IBAction func deleteSection(_ sender: UIButton) { 2 3 if sectionTitleArray.isEmpty { 4 return 5 } 6 7 sectionTitleArray.remove(at: 0) 8 dataArrayGroup.remove(at: 0) 9 tableView.deleteSections(IndexSet(integer: 0), with: .automatic) 10}

TextFieldに入力した文字列と同じsectionを削除する場合以下の様に書いてください。

swift

1@IBAction func deleteSection(_ sender: UIButton) { 2 3 guard let text = textField.text, 4 !text.isEmpty, 5 !sectionTitleArray.isEmpty, 6 let indext = sectionTitleArray.index(of: text) else { 7 return 8 } 9 10 sectionTitleArray.remove(at: indext) 11 dataArrayGroup.remove(at: indext) 12 tableView.deleteSections(IndexSet(integer: indext), with: .automatic) 13 14}

投稿2017/01/06 14:38

編集2017/01/07 04:44
_Kentarou

総合スコア8490

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

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

blakekei

2017/01/06 14:50

ありがとうございます 教えて貰えた通りにやったら出来ました それともう一つ質問ですがこれと同じ要領でやればsectionも削除できるようになりますか?
_Kentarou

2017/01/06 14:56

sectionはどのsectionを削除するのかスワイプの様に決められないのですが、blakekeiさん的にはどの様なアクションで削除しようと考えていますか?
blakekei

2017/01/06 15:03

まだswiftを勉強したばかりでスワイプ以外の削除の仕方がわからないのでまた質問の丸投げと思われるかもしれませんがどのアクションでもいいのでsectionを削除できる形にして頂けたら嬉しいです。 本当に何もかも無知の状態で回答ばかり求めてすいません
_Kentarou

2017/01/06 15:13

回答に追記しました、確認してみてください。
blakekei

2017/01/06 15:28

ありがとうございました イメージ通りに出来ました あと、複数のsectionを作った場合に例えば2つ目のsectionだけ消したい場合時など特定のsectionを選択して削除する場合にはどの部分を書き換えればいいですか?
_Kentarou

2017/01/06 15:30

sectionTitleArray.remove(at: 消したいsectionの番号) dataArrayGroup.remove(at: 消したいsectionの番号) tableView.deleteSections(IndexSet(integer: 消したいsectionの番号), with: .automatic) 消したいsectionの番号を指定してください。
blakekei

2017/01/06 15:43

説明不足ですいません 固定した形よりいくつかのsectionをアプリの操作時に作ってから打ち間違いか何かで消したくなった時にタップか何かの操作でその消したいsectionを選択して消せるようにしたいのですがxcode上このような形は出来ないのでしょうか?
blakekei

2017/01/07 03:35

sectionTitleArray.remove(at: deletetext) dataArrayGroup.remove(at: deletetext) tableView.deleteSections(IndexSet(integer: deletetext), with: .automatic) } @IBOutlet weak var deletetext: UITextField! 自分なりにこう考えてテキストフィールドを追加してコードを付け足して見たのですがエラーが出来たのですがどう書けばいいでしょうか? またこれ以外にもっと簡単な方法ありますか? もしよかったら教えて頂けませんか?
_Kentarou

2017/01/07 04:04

sectionタイトルを指定してそのsectionを消したいということですか?
blakekei

2017/01/07 04:13

自分にはそれしか思いつかなかったですがやりたいことはそういうことです。 他にもっと簡単な方法ありますか?
_Kentarou

2017/01/07 04:45

回答に追記しました。
blakekei

2017/01/07 05:01

ありがとうございました 自分のイメージしている通りに出来ました 何度も何度も質問してご迷惑おかけしていると思いますがまた質問がある際はご迷惑とおかけしますがまたよろしくお願いします。 本当にありがとうございました。
blakekei

2017/01/07 13:45

他の質問ですがalerttextを自分なりにやって見たのですがエラーが起きてしまったんですがもしよかったら「alerttextについて」で質問しているコードを修正して頂けませんか? お忙しいとは思いますがよろしくお願いいたします。
blakekei

2017/01/07 16:07

rowの部分を2行まで用意できたのですが入力したデータが反映されないですがどこが間違ってるか修正して頂けませんか?
blakekei

2017/01/08 01:39

おはようございます rowの部分にalerttextを二つ用意できたのですが1行のrowの中に二つのデータを収めたいのですがどうしても2行になってしまうのですがどうしたらいいでしょうか?
blakekei

2017/01/08 09:59

labelを貼り付けても既存コードだとlabelの内容が追加したrowに反映されないのですがこの場合何か書き変えなきゃいけないのでしょうか?
guest

0

以前にもお伝えしましたが、プロトコルで宣言している場合はデータ表示は、この記述が必要になります。

override func viewDidLoad() { super.viewDidLoad() // DataSourceの設定をする. tableView.dataSource = self // Delegateを設定する. tableView.delegate = self }

ご指定の箇所で、削除する方法

sectionTitleArray.remove(at: section)

以前にもお伝えしましたが、削除した場合は、このコードが必要になります。

tableView.reloadData() または tableView.endUpdates()

理由はtableview内のDataSourceを変更した場合に更新処理を必要とするからです。

投稿2017/01/06 14:39

編集2017/01/06 14:54
Dbank

総合スコア120

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

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

_Kentarou

2017/01/06 14:45

// DataSourceの設定をする. tableView.dataSource = self // Delegateを設定する. tableView.delegate = self 上記はStoryboardで設定していると思われますよ。
_Kentarou

2017/01/06 14:53

tableView.reloadData() または tableView.endUpdates() 上記のメソッドは以下で削除&リロードしているので必要ないですよ。 tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
Dbank

2017/01/06 15:00 編集

確かにその通りですね。 コメントに//この部分です。と記載していたので、何かしらのButtonアクションを実施した際に起こしたい挙動を推測しました。 以前のやりとりでは、文字のデータを代入したいと記載文がありましたので。今回のフラグとは異なりますが。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問