teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

s

2017/04/16 02:40

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,3 +1,10 @@
1
+ 検索はかかっていますが、表示する配列数を返すところが`return Title.count`となっていて常に`3`になってしまっています。
2
+
3
+ あとはセルを作成するところも、検索した配列を見ていないのでダメですね。
4
+
5
+
6
+ ---
7
+
1
8
  画像名、タイトルなど別々の配列として定義していますが、クラスや構造体を以下の様に定義した方が良いと思います。
2
9
 
3
10
  ```swift

1

s

2017/04/16 02:40

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,9 +1,105 @@
1
- 検索した配列の数を返さないセルの数が変更されなので以下の様にしてみてください。
1
+ 画像名、タイトルなど別々の配列として定義してますが、クラスや構造体を以下の様に定義た方が良と思います
2
2
 
3
3
  ```swift
4
+ struct CatData {
5
+ /// タイトル
6
+ var title: String
7
+ /// 画像名
8
+ var imageName: String
4
- //セルの個数を指定するデリゲードメソッド(必須)
9
+ /// セルの説明
10
+ var description: String
11
+ }
12
+ ```
13
+
14
+ それをふまえてコードを変更してみました、確認してみてください。
15
+
16
+ ```swift
17
+ import UIKit
18
+
19
+ struct CatData {
20
+ /// タイトル
21
+ var title: String
22
+ /// 画像名
23
+ var imageName: String
24
+ /// セルの説明
25
+ var description: String
26
+ }
27
+
5
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
28
+ class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
6
29
 
30
+ //TableViewのoutlet
31
+ @IBOutlet weak var allBusTableView: UITableView!
32
+
33
+ //サーチバーのoutlet
34
+ @IBOutlet weak var searchBar1: UISearchBar!
35
+
36
+ // CatDataの配列
37
+ var catDataArray: [CatData] = []
38
+
39
+ //searchBar検索結果配列
40
+ var searchResult: [CatData] = []
41
+
42
+ override func viewDidLoad() {
43
+ super.viewDidLoad()
44
+
45
+ searchBar1.delegate = self
46
+ searchBar1.enablesReturnKeyAutomatically = true
47
+
48
+ createData()
49
+ }
50
+
51
+ func createData() {
52
+ catDataArray.append(CatData(title: "猫1", imageName: "cat1.jpg", description: "猫1です"))
53
+ catDataArray.append(CatData(title: "猫2", imageName: "cat2.jpg", description: "猫2です"))
54
+ catDataArray.append(CatData(title: "猫3", imageName: "cat3.jpg", description: "猫3です"))
55
+ searchResult = catDataArray
56
+ }
57
+
58
+ //セルの個数を指定するデリゲードメソッド(必須)
59
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
60
+
7
- return searchResult.count
61
+ return searchResult.count
62
+ }
63
+
64
+ //セルの値を設定するデリゲードメソッド(必須)
65
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
66
+ //セルを取得
67
+ let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell
68
+
69
+ let catData = searchResult[indexPath.row]
70
+ cell.setCell(catData.imageName, titleText: catData.title, descriptionText: catData.imageName)
71
+
72
+ return cell
73
+ }
74
+
75
+ func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
76
+ searchBar.endEditing(true)
77
+ }
78
+
79
+ //検索結果ボタン押下時のメソッド
80
+ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
81
+
82
+ //検索結果配列を空にする
83
+ searchResult.removeAll()
84
+
85
+ if searchText.isEmpty {
86
+ searchResult = catDataArray
87
+ } else {
88
+
89
+ searchResult = catDataArray.filter {
90
+
91
+ // 前方一致の場合
92
+ // $0.title.hasPrefix(searchText)
93
+
94
+ // 後方一致の場合
95
+ // $0.title.hasSuffix(searchText)
96
+
97
+ // 部分一致の場合
98
+ $0.title.contains(searchText)
99
+ }
100
+ }
101
+ allBusTableView.reloadData()
102
+ }
8
103
  }
104
+
9
105
  ```