質問編集履歴
3
add picture
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,6 +12,18 @@
|
|
12
12
|
|
13
13
|
今の環境だとベストなものは何でしょうか?
|
14
14
|
|
15
|
+
|
16
|
+
|
17
|
+
入力してenterを押す前は上部にあり
|
18
|
+
|
19
|
+
![入力前](087866808e7e23821c4f0872ef9c4f0a.png)
|
20
|
+
|
21
|
+
enterを押してcellに情報が入ると下にスクロールした最下部に留まってしまいます。
|
22
|
+
|
23
|
+
再度文字を検索しても変わりません。
|
24
|
+
|
25
|
+
![Enter後](925e887e21cfb67a50815ca3d121cd7d.png)
|
26
|
+
|
15
27
|
```ここに言語を入力
|
16
28
|
|
17
29
|
import UIKit
|
2
add code
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,15 +14,71 @@
|
|
14
14
|
|
15
15
|
```ここに言語を入力
|
16
16
|
|
17
|
+
import UIKit
|
18
|
+
|
19
|
+
import PKHUD
|
20
|
+
|
21
|
+
|
22
|
+
|
17
|
-
class SearchViewController: UIViewController, UISearchBarDelegate {
|
23
|
+
class MovieSearchViewController: UIViewController, UISearchBarDelegate {
|
24
|
+
|
25
|
+
|
26
|
+
|
18
|
-
|
27
|
+
@IBOutlet weak var table: UITableView!
|
19
|
-
|
20
|
-
|
28
|
+
|
21
|
-
@IBOutlet weak var search: UISearchBar!
|
29
|
+
@IBOutlet weak var search: UISearchBar!
|
30
|
+
|
22
|
-
|
31
|
+
@IBOutlet weak var indicater: UIActivityIndicatorView!
|
32
|
+
|
33
|
+
|
34
|
+
|
23
|
-
|
35
|
+
private var items:[MovieItem] = [MovieItem]()
|
36
|
+
|
24
|
-
|
37
|
+
var alertController: UIAlertController!
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
override func viewDidLoad() {
|
42
|
+
|
43
|
+
super.viewDidLoad()
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
self.setupTable()
|
48
|
+
|
49
|
+
self.setupNavigationBackground()
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
//エラーアラート
|
58
|
+
|
59
|
+
func alert(title:String, message:String) {
|
60
|
+
|
61
|
+
alertController = UIAlertController(title: title,
|
62
|
+
|
63
|
+
message: message,
|
64
|
+
|
65
|
+
preferredStyle: .alert)
|
66
|
+
|
67
|
+
alertController.addAction(UIAlertAction(title: "OK",
|
68
|
+
|
69
|
+
style: .default,
|
70
|
+
|
71
|
+
handler: nil))
|
72
|
+
|
73
|
+
present(alertController, animated: true)
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//SearchButtounClicked
|
80
|
+
|
25
|
-
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
81
|
+
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
26
82
|
|
27
83
|
view.endEditing(true)
|
28
84
|
|
@@ -32,13 +88,13 @@
|
|
32
88
|
|
33
89
|
}
|
34
90
|
|
35
|
-
|
36
|
-
|
91
|
+
|
92
|
+
|
37
|
-
func fechData(){
|
93
|
+
func fechData(){
|
38
|
-
|
39
|
-
|
40
|
-
|
94
|
+
|
95
|
+
|
96
|
+
|
41
|
-
search.resignFirstResponder()
|
97
|
+
search.resignFirstResponder()
|
42
98
|
|
43
99
|
|
44
100
|
|
@@ -48,12 +104,206 @@
|
|
48
104
|
|
49
105
|
}
|
50
106
|
|
107
|
+
|
108
|
+
|
109
|
+
guard let query = text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
|
110
|
+
|
111
|
+
print("パーセントエンコーディング失敗")
|
112
|
+
|
113
|
+
return
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
//LoadingAction Start
|
120
|
+
|
121
|
+
HUD.show(.progress)
|
122
|
+
|
123
|
+
items.removeAll()
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
URLSession.shared.dataTask(with: URL(string: "https://api.themoviedb.org/3/search/movie?api_key=MYAPIKEY&language=ja&query=(query)&page=1")!,
|
128
|
+
|
129
|
+
completionHandler: {data, response, error in
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
guard let data = data else {
|
134
|
+
|
135
|
+
print(error?.localizedDescription ?? "Unknown error")
|
136
|
+
|
137
|
+
return
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
51
|
-
|
143
|
+
var result: MovieStruct?
|
144
|
+
|
145
|
+
do {
|
146
|
+
|
147
|
+
result = try JSONDecoder().decode(MovieStruct.self, from: data)
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
catch{
|
152
|
+
|
153
|
+
print("JSON perse error")
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
guard let finalResult = result else {
|
160
|
+
|
161
|
+
return
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
let newMovies = finalResult.results
|
168
|
+
|
169
|
+
self.items.append(contentsOf: newMovies)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
DispatchQueue.main.async {
|
174
|
+
|
175
|
+
self.table.reloadData()
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
//検索エラーメッセージ
|
180
|
+
|
181
|
+
if newMovies.isEmpty {
|
182
|
+
|
183
|
+
self.alert(title: "エラー",message: "該当する映画がありませんでした")
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
//LoadingAction Stop
|
190
|
+
|
191
|
+
HUD.flash(.progress)
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}).resume()
|
52
196
|
|
53
197
|
}
|
54
198
|
|
55
199
|
}
|
56
200
|
|
201
|
+
|
202
|
+
|
203
|
+
extension MovieSearchViewController {
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
private func setupTable() {
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
table.delegate = self
|
212
|
+
|
213
|
+
table.dataSource = self
|
214
|
+
|
215
|
+
search.delegate = self
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
table.register(TableViewCell.nib(), forCellReuseIdentifier: TableViewCell.identifier)
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
|
226
|
+
|
227
|
+
search.text = ""
|
228
|
+
|
229
|
+
search.autocapitalizationType = .none
|
230
|
+
|
231
|
+
return true
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
extension MovieSearchViewController: UITableViewDataSource {
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
244
|
+
|
245
|
+
return items.count
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
252
|
+
|
253
|
+
let cell = table.dequeueReusableCell(withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
|
254
|
+
|
255
|
+
cell.setcell(item: items[indexPath.row])
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
return cell
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
extension MovieSearchViewController: UITableViewDelegate {
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
272
|
+
|
273
|
+
tableView.deselectRow(at: indexPath, animated: true)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
self.hidesBottomBarWhenPushed = true
|
278
|
+
|
279
|
+
performSegue(withIdentifier: "registerControl", sender: items[indexPath.row])
|
280
|
+
|
281
|
+
self.hidesBottomBarWhenPushed = false
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
290
|
+
|
291
|
+
if segue.identifier == "registerControl" {
|
292
|
+
|
293
|
+
let nextVC:RegisterViewController = segue.destination as! RegisterViewController
|
294
|
+
|
295
|
+
if let selectedMovies = sender as? MovieItem {
|
296
|
+
|
297
|
+
nextVC.movie = selectedMovies
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
|
57
307
|
```
|
58
308
|
|
59
309
|
### 余談
|
1
add code
test
CHANGED
File without changes
|
test
CHANGED
@@ -55,3 +55,15 @@
|
|
55
55
|
}
|
56
56
|
|
57
57
|
```
|
58
|
+
|
59
|
+
### 余談
|
60
|
+
|
61
|
+
SearchBarに英字入力すると一文字目が大文字から始まりますが
|
62
|
+
|
63
|
+
[Search Barの先頭文字を小文字にしたい](https://teratail.com/questions/291134)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
こちらを参考に変えてみたのですが反映されません。
|
68
|
+
|
69
|
+
大文字からでも困らないので余談ですがわかる方がいらっしゃいましたら教えてもらいたいです。
|