質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2281閲覧

Swift3.0でTableViewCellの中にある文字列をSearchBarで検索し、表示させたい。

Shintarosu

総合スコア7

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/04/15 15:22

###前提・実現したいこと
TableViewのCellにある文字列をUISearchBarで検索をしその文字列がヒットしたら表示されるというプログラムを現在書いているのですが、どうにも上手く動きません。
定数Titleの中にある文字列がヒットしたら表示させるという事を func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {}の中に書いてみたのですが、自分が見た所どこがおかしいのかさっぱりです。。

原因がわからないので是非ご教授よろしくお願いいたします。


###該当のソースコード

Swift

1 2import UIKit 3 4class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{ 5 6 //TableViewのoutlet 7 @IBOutlet weak var allBusTableView: UITableView! 8 9 //サーチバーのoutlet 10 @IBOutlet weak var searchBar1: UISearchBar! 11 12 //searchBar検索結果配列 13 var searchResult = [String]() 14 15 let imageFile = ["cat1.jpg", "cat2.jpg", "dog1.jpg", "dog2.jpg"] 16 //タイトルの説明 17 let Title = ["猫1","猫2","猫3"] 18 19 //セルの説明 20 let imageDescription = [ "猫1です","猫2です","猫3です"] 21 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 25 searchBar1.delegate = self 26 27 searchBar1.enablesReturnKeyAutomatically = true 28 29 searchResult = Title 30 } 31 32 override func didReceiveMemoryWarning() { 33 super.didReceiveMemoryWarning() 34 } 35 36 //セルの個数を指定するデリゲードメソッド(必須) 37 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 38 39 return Title.count 40 } 41 42 //セルの値を設定するデリゲードメソッド(必須) 43 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 44 //セルを取得 45 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! CustomTableViewCell 46 47 cell.setCell(imageFile[indexPath.row], titleText: Title[indexPath.row], descriptionText: imageDescription[indexPath.row]) 48 49 return cell 50 } 51 52 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 53 searchBar.endEditing(true) 54 } 55 56 //検索結果ボタン押下時のメソッド 57 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 58 //検索結果配列を空にする 59 searchResult.removeAll() 60 61 if(searchBar1.text == "") { 62 searchResult = Title 63 } else { 64 for data in Title { 65 if data.contains(searchBar1.text!) { 66 searchResult.append(data) 67 } 68 } 69 } 70 allBusTableView.reloadData() 71 } 72}

###補足情報(言語/FW/ツール等のバージョンなど)
xcode8.0/swift3.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

検索はかかっていますが、表示する配列数を返すところがreturn Title.countとなっていて常に3になってしまっています。

あとはセルを作成するところも、検索した配列を見ていないのでダメですね。


画像名、タイトルなど別々の配列として定義していますが、クラスや構造体を以下の様に定義した方が良いと思います。

swift

1struct CatData { 2 /// タイトル 3 var title: String 4 /// 画像名 5 var imageName: String 6 /// セルの説明 7 var description: String 8}

それをふまえてコードを変更してみました、確認してみてください。

swift

1import UIKit 2 3struct CatData { 4 /// タイトル 5 var title: String 6 /// 画像名 7 var imageName: String 8 /// セルの説明 9 var description: String 10} 11 12class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { 13 14 //TableViewのoutlet 15 @IBOutlet weak var allBusTableView: UITableView! 16 17 //サーチバーのoutlet 18 @IBOutlet weak var searchBar1: UISearchBar! 19 20 // CatDataの配列 21 var catDataArray: [CatData] = [] 22 23 //searchBar検索結果配列 24 var searchResult: [CatData] = [] 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 searchBar1.delegate = self 30 searchBar1.enablesReturnKeyAutomatically = true 31 32 createData() 33 } 34 35 func createData() { 36 catDataArray.append(CatData(title: "猫1", imageName: "cat1.jpg", description: "猫1です")) 37 catDataArray.append(CatData(title: "猫2", imageName: "cat2.jpg", description: "猫2です")) 38 catDataArray.append(CatData(title: "猫3", imageName: "cat3.jpg", description: "猫3です")) 39 searchResult = catDataArray 40 } 41 42 //セルの個数を指定するデリゲードメソッド(必須) 43 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 44 45 return searchResult.count 46 } 47 48 //セルの値を設定するデリゲードメソッド(必須) 49 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 50 //セルを取得 51 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell 52 53 let catData = searchResult[indexPath.row] 54 cell.setCell(catData.imageName, titleText: catData.title, descriptionText: catData.imageName) 55 56 return cell 57 } 58 59 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 60 searchBar.endEditing(true) 61 } 62 63 //検索結果ボタン押下時のメソッド 64 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 65 66 //検索結果配列を空にする 67 searchResult.removeAll() 68 69 if searchText.isEmpty { 70 searchResult = catDataArray 71 } else { 72 73 searchResult = catDataArray.filter { 74 75 // 前方一致の場合 76 // $0.title.hasPrefix(searchText) 77 78 // 後方一致の場合 79 // $0.title.hasSuffix(searchText) 80 81 // 部分一致の場合 82 $0.title.contains(searchText) 83 } 84 } 85 allBusTableView.reloadData() 86 } 87} 88

投稿2017/04/16 02:16

編集2017/04/16 02:40
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Shintarosu

2017/04/16 13:05

ご返信ありがとうございます! わかりやすくご教授してもらいありがとうございます!! クラスや構造体での使い方も理解できました! プログラムを書き換えて実行した所、上手く動きました! 本当にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問