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] ←これがエラーになるんですけどどう書き換えたらいいですか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。