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

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

新規登録して質問してみよう
ただいま回答率
85.48%
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

解決済

1回答

1840閲覧

tableview section でエラー

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/01 14:13

編集2017/01/03 15:13

swift

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

ビルドをするとfunc tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitleArray[section] ←これがエラーになるんですけどどう書き換えたらいいですか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

やりたいことがハッキリとは分かりませんがこの様にするといかがでしょうか?

swift

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

投稿2017/01/03 16:09

編集2017/01/03 16:12
_Kentarou

総合スコア8490

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問