回答編集履歴
2
s
test
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
検索はかかっていますが、表示する配列数を返すところが`return Title.count`となっていて常に`3`になってしまっています。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
あとはセルを作成するところも、検索した配列を見ていないのでダメですね。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
---
|
12
|
+
|
13
|
+
|
14
|
+
|
1
15
|
画像名、タイトルなど別々の配列として定義していますが、クラスや構造体を以下の様に定義した方が良いと思います。
|
2
16
|
|
3
17
|
|
1
s
test
CHANGED
@@ -1,17 +1,209 @@
|
|
1
|
-
|
1
|
+
画像名、タイトルなど別々の配列として定義していますが、クラスや構造体を以下の様に定義した方が良いと思います。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
5
|
```swift
|
6
6
|
|
7
|
+
struct CatData {
|
8
|
+
|
9
|
+
/// タイトル
|
10
|
+
|
11
|
+
var title: String
|
12
|
+
|
13
|
+
/// 画像名
|
14
|
+
|
15
|
+
var imageName: String
|
16
|
+
|
7
|
-
//セルの
|
17
|
+
/// セルの説明
|
8
|
-
|
9
|
-
|
18
|
+
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
re
|
19
|
+
var description: String
|
14
20
|
|
15
21
|
}
|
16
22
|
|
17
23
|
```
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
それをふまえてコードを変更してみました、確認してみてください。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```swift
|
32
|
+
|
33
|
+
import UIKit
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
struct CatData {
|
38
|
+
|
39
|
+
/// タイトル
|
40
|
+
|
41
|
+
var title: String
|
42
|
+
|
43
|
+
/// 画像名
|
44
|
+
|
45
|
+
var imageName: String
|
46
|
+
|
47
|
+
/// セルの説明
|
48
|
+
|
49
|
+
var description: String
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
//TableViewのoutlet
|
60
|
+
|
61
|
+
@IBOutlet weak var allBusTableView: UITableView!
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
//サーチバーのoutlet
|
66
|
+
|
67
|
+
@IBOutlet weak var searchBar1: UISearchBar!
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// CatDataの配列
|
72
|
+
|
73
|
+
var catDataArray: [CatData] = []
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
//searchBar検索結果配列
|
78
|
+
|
79
|
+
var searchResult: [CatData] = []
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
override func viewDidLoad() {
|
84
|
+
|
85
|
+
super.viewDidLoad()
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
searchBar1.delegate = self
|
90
|
+
|
91
|
+
searchBar1.enablesReturnKeyAutomatically = true
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
createData()
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
func createData() {
|
102
|
+
|
103
|
+
catDataArray.append(CatData(title: "猫1", imageName: "cat1.jpg", description: "猫1です"))
|
104
|
+
|
105
|
+
catDataArray.append(CatData(title: "猫2", imageName: "cat2.jpg", description: "猫2です"))
|
106
|
+
|
107
|
+
catDataArray.append(CatData(title: "猫3", imageName: "cat3.jpg", description: "猫3です"))
|
108
|
+
|
109
|
+
searchResult = catDataArray
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
//セルの個数を指定するデリゲードメソッド(必須)
|
116
|
+
|
117
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
return searchResult.count
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
//セルの値を設定するデリゲードメソッド(必須)
|
128
|
+
|
129
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
130
|
+
|
131
|
+
//セルを取得
|
132
|
+
|
133
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
let catData = searchResult[indexPath.row]
|
138
|
+
|
139
|
+
cell.setCell(catData.imageName, titleText: catData.title, descriptionText: catData.imageName)
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
return cell
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
150
|
+
|
151
|
+
searchBar.endEditing(true)
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
//検索結果ボタン押下時のメソッド
|
158
|
+
|
159
|
+
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
//検索結果配列を空にする
|
164
|
+
|
165
|
+
searchResult.removeAll()
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
if searchText.isEmpty {
|
170
|
+
|
171
|
+
searchResult = catDataArray
|
172
|
+
|
173
|
+
} else {
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
searchResult = catDataArray.filter {
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
// 前方一致の場合
|
182
|
+
|
183
|
+
// $0.title.hasPrefix(searchText)
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
// 後方一致の場合
|
188
|
+
|
189
|
+
// $0.title.hasSuffix(searchText)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// 部分一致の場合
|
194
|
+
|
195
|
+
$0.title.contains(searchText)
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
allBusTableView.reloadData()
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
```
|