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

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

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

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

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

0回答

1197閲覧

sectionにdelete機能を持たしたい

blakekei

総合スコア35

TableView

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

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/04 13:09

swift

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

sectionとrowをスワイプした時に入力した値をdelete出来るようにしたいです
それとrowをアラームテキストでデータを入力してもデータが反映されないのでそれを反映出来るようにしたいです

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問