質問編集履歴

2

改善

2019/11/02 09:36

投稿

Larry
Larry

スコア28

test CHANGED
File without changes
test CHANGED
@@ -54,226 +54,220 @@
54
54
 
55
55
 
56
56
 
57
+
58
+
59
+ ```swift
60
+
61
+ import UIKit
62
+
63
+ class ViewController: UIViewController {
64
+
65
+
66
+
67
+ @IBOutlet weak var tableView: UITableView!
68
+
69
+ var searchController: UISearchController!
70
+
71
+ var words = [String]()
72
+
73
+ var searchResults:[String] = []
74
+
75
+
76
+
77
+ override func viewDidLoad() {
78
+
79
+ super.viewDidLoad()
80
+
81
+ //高さ自動調整
82
+
83
+ tableView.rowHeight = UITableView.automaticDimension
84
+
85
+ //余分なセルを非表示
86
+
87
+ self.tableView.tableFooterView = UIView()
88
+
89
+ navigationItem.leftBarButtonItem = editButtonItem
90
+
91
+ let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:#selector(onAddTapped(_:)))
92
+
93
+ navigationItem.rightBarButtonItem = addButton
94
+
95
+
96
+
97
+ setupSearchBar()
98
+
99
+ }
100
+
101
+
102
+
103
+ func setupSearchBar() {
104
+
105
+ searchController = UISearchController(searchResultsController: nil)
106
+
107
+ searchController.searchResultsUpdater = self
108
+
109
+ searchController.searchBar.delegate = self
110
+
111
+
112
+
113
+ searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 0, height: 60)
114
+
115
+ //searchController.searchBar.sizeToFit()
116
+
117
+ searchController.searchBar.placeholder = "WAROTA"
118
+
119
+ searchController.searchBar.tintColor = UIColor.red
120
+
121
+ searchController.searchBar.keyboardType = UIKeyboardType.default
122
+
123
+
124
+
125
+ searchController.dimsBackgroundDuringPresentation = false
126
+
127
+ searchController.hidesNavigationBarDuringPresentation = false
128
+
129
+
130
+
131
+ tableView.tableHeaderView = searchController.searchBar
132
+
133
+ }
134
+
135
+
136
+
137
+ @objc func onAddTapped(_ sender: Any) {
138
+
139
+ let alert = UIAlertController(title: "単語を登録", message: "Enter Chinese",preferredStyle: .alert)
140
+
141
+ alert.addTextField {(ChineseTH) in ChineseTH.placeholder = "words" }
142
+
143
+ let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
144
+
145
+ let okAction = UIAlertAction(title: "Add", style: .default) { (_) in
146
+
147
+
148
+
149
+ guard let word = alert.textFields?.first?.text, word.count > 0 else { return }
150
+
151
+ self.insertCellRow(word)
152
+
153
+
154
+
155
+ }
156
+
157
+
158
+
159
+ alert.addAction(cancelAction)
160
+
161
+ alert.addAction(okAction)
162
+
163
+
164
+
165
+ self.present(alert, animated: true, completion: nil)
166
+
167
+
168
+
169
+ }
170
+
171
+
172
+
173
+ func insertCellRow(_ word: String){
174
+
175
+ words.insert(word, at: 0)
176
+
177
+ let indexPath = IndexPath(row: 0, section: 0)//初期化
178
+
179
+ tableView.insertRows(at: [indexPath], with: .automatic)//新しいセルを挿入しているだけ
180
+
181
+
182
+
183
+ }
184
+
185
+
186
+
187
+ }
188
+
189
+
190
+
191
+ // MARK: - tabeleView
192
+
193
+ extension ViewController: UITableViewDelegate, UITableViewDataSource {
194
+
195
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
196
+
197
+ return words.count
198
+
199
+ }
200
+
201
+
202
+
203
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
204
+
205
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
206
+
207
+ let word = words[indexPath.row]
208
+
209
+ cell?.textLabel?.text = word
210
+
211
+ return cell!
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ // MARK: - searchBar
220
+
221
+ extension ViewController: UISearchResultsUpdating {
222
+
223
+ func updateSearchResults(for searchController: UISearchController) {
224
+
225
+ self.searchResults = words.filter{
226
+
227
+ $0.lowercased().contains(searchController.searchBar.text!.lowercased())
228
+
229
+ }
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+ // MARK: - searchBar
238
+
239
+ extension ViewController: UISearchBarDelegate {
240
+
241
+
242
+
243
+ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
244
+
245
+ }
246
+
247
+
248
+
249
+ func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
250
+
251
+ searchBar.text = ""
252
+
253
+ tableView.reloadData()
254
+
255
+ }
256
+
257
+
258
+
259
+ func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
57
267
  ```
58
268
 
59
269
 
60
270
 
61
- ### 該当のソースコード
62
-
63
-
64
-
65
- ```swift
66
-
67
- import UIKit
68
-
69
- class ViewController: UIViewController {
70
-
71
-
72
-
73
- @IBOutlet weak var tableView: UITableView!
74
-
75
- var searchController: UISearchController!
76
-
77
- var words = [String]()
78
-
79
- var searchResults:[String] = []
80
-
81
-
82
-
83
- override func viewDidLoad() {
84
-
85
- super.viewDidLoad()
86
-
87
- //高さ自動調整
88
-
89
- tableView.rowHeight = UITableView.automaticDimension
90
-
91
- //余分なセルを非表示
92
-
93
- self.tableView.tableFooterView = UIView()
94
-
95
- navigationItem.leftBarButtonItem = editButtonItem
96
-
97
- let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:#selector(onAddTapped(_:)))
98
-
99
- navigationItem.rightBarButtonItem = addButton
100
-
101
-
102
-
103
- setupSearchBar()
104
-
105
- }
106
-
107
-
108
-
109
- func setupSearchBar() {
110
-
111
- searchController = UISearchController(searchResultsController: nil)
112
-
113
- searchController.searchResultsUpdater = self
114
-
115
- searchController.searchBar.delegate = self
116
-
117
-
118
-
119
- searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 0, height: 60)
120
-
121
- //searchController.searchBar.sizeToFit()
122
-
123
- searchController.searchBar.placeholder = "WAROTA"
124
-
125
- searchController.searchBar.tintColor = UIColor.red
126
-
127
- searchController.searchBar.keyboardType = UIKeyboardType.default
128
-
129
-
130
-
131
- searchController.dimsBackgroundDuringPresentation = false
132
-
133
- searchController.hidesNavigationBarDuringPresentation = false
134
-
135
-
136
-
137
- tableView.tableHeaderView = searchController.searchBar
138
-
139
- }
140
-
141
-
142
-
143
- @objc func onAddTapped(_ sender: Any) {
144
-
145
- let alert = UIAlertController(title: "単語を登録", message: "Enter Chinese",preferredStyle: .alert)
146
-
147
- alert.addTextField {(ChineseTH) in ChineseTH.placeholder = "words" }
148
-
149
- let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
150
-
151
- let okAction = UIAlertAction(title: "Add", style: .default) { (_) in
152
-
153
-
154
-
155
- guard let word = alert.textFields?.first?.text, word.count > 0 else { return }
156
-
157
- self.insertCellRow(word)
158
-
159
-
160
-
161
- }
162
-
163
-
164
-
165
- alert.addAction(cancelAction)
166
-
167
- alert.addAction(okAction)
168
-
169
-
170
-
171
- self.present(alert, animated: true, completion: nil)
172
-
173
-
174
-
175
- }
176
-
177
-
178
-
179
- func insertCellRow(_ word: String){
180
-
181
- words.insert(word, at: 0)
182
-
183
- let indexPath = IndexPath(row: 0, section: 0)//初期化
184
-
185
- tableView.insertRows(at: [indexPath], with: .automatic)//新しいセルを挿入しているだけ
186
-
187
-
188
-
189
- }
190
-
191
-
192
-
193
- }
194
-
195
-
196
-
197
- // MARK: - tabeleView
198
-
199
- extension ViewController: UITableViewDelegate, UITableViewDataSource {
200
-
201
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
202
-
203
- return words.count
204
-
205
- }
206
-
207
-
208
-
209
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
210
-
211
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
212
-
213
- let word = words[indexPath.row]
214
-
215
- cell?.textLabel?.text = word
216
-
217
- return cell!
218
-
219
- }
220
-
221
- }
222
-
223
-
224
-
225
- // MARK: - searchBar
226
-
227
- extension ViewController: UISearchResultsUpdating {
228
-
229
- func updateSearchResults(for searchController: UISearchController) {
230
-
231
- self.searchResults = words.filter{
232
-
233
- $0.lowercased().contains(searchController.searchBar.text!.lowercased())
234
-
235
- }
236
-
237
- }
238
-
239
- }
240
-
241
-
242
-
243
- // MARK: - searchBar
244
-
245
- extension ViewController: UISearchBarDelegate {
246
-
247
-
248
-
249
- func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
250
-
251
- }
252
-
253
-
254
-
255
- func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
256
-
257
- searchBar.text = ""
258
-
259
- tableView.reloadData()
260
-
261
- }
262
-
263
-
264
-
265
- func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
266
-
267
- }
268
-
269
- }
270
-
271
-
272
-
273
- ```
274
-
275
-
276
-
277
271
  ### 補足情報(FW/ツールのバージョンなど)
278
272
 
279
273
  xcode11.1

1

改善

2019/11/02 09:35

投稿

Larry
Larry

スコア28

test CHANGED
File without changes
test CHANGED
@@ -274,16 +274,6 @@
274
274
 
275
275
 
276
276
 
277
- ### 試したこと
278
-
279
-
280
-
281
- ここに問題に対して試したことを記載してください。
282
-
283
-
284
-
285
277
  ### 補足情報(FW/ツールのバージョンなど)
286
278
 
287
-
288
-
289
- ここにより詳細な情報を記載してください。
279
+ xcode11.1