2017-05-29 31 views
0

searchBarでカスタムtableViewControllerを作成しています。カスタムセルクラス "DataCell"を作成しました。カスタムテーブルビューに検索バーを追加

次のコードでは、searchBarは表示されず、ラベルの配列は表示されません。

どうすればこの問題を解決できますか? iOSの8のよう

import UIKit 

class DataCell: UITableViewCell{ 

    @IBOutlet weak var label: UILabel! 

} 

class SearchController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { 

    @IBOutlet weak var tableView: UITableView! 
    @IBOutlet weak var searchBar: UISearchBar! 

    var isSearching = false 


    var data = ["a","b","c","d","e"] 
    var filterData = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 

     searchBar.delegate = self 
     searchBar.returnKeyType = UIReturnKeyType.done 

    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

      if isSearching{ 
       return filterData.count 
      } 

     return data.count 
    } 



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) as? DataCell { 

      let text: String! 

      if isSearching { 

       text = filterData[indexPath.row] 
      } else { 
       text = data[indexPath.row] 
      } 

      return cell 

     } else { 
      return UITableViewCell() 
     } 
    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     if searchBar.text == nil || searchBar.text == ""{ 
      isSearching = false 

      view.endEditing(true) 

      tableView.reloadData() 
     }else { 
      isSearching = true 

      filterData = data.filter({$0 == searchBar.text}) 

      tableView.reloadData() 
     } 
    } 

} 
+0

あなたが異なることにより、何を意味して? – risa8

+0

@ risa8ここにあなたの質問にエラーを追加する必要があります –

+0

エラー – risa8

答えて

1

、あなたは2クラスを作成する必要のある[UISearchController][1]を使用する必要があります。 SearchControllerおよびResultsControllerである。検索クラスのために、そして、

import UIKit 

class DataCell: UITableViewCell { 
    @IBOutlet weak var label: UILabel! 

    func configureCell(_ text: String) { 
     label.text = text 
    } 
} 

::私たちは、共通のUITableViewクラスを作成することから始め

class SearchController: UIViewController { 

    @IBOutlet weak var tableView: UITableView! 
    var searchController: UISearchController! 
    var resultController: ResultController? 

    var data = ["a","b","c","d","e"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.register(TableCell.self, forCellReuseIdentifier: "DataCell") 
     tableView.register(UINib(nibName: "DataCell", bundle: Bundle.main), forCellReuseIdentifier: "DataCell") 

     // Search Results 
     resultController = ResultController() 
     setupSearchControllerWith(resultController!) 
     if #available(iOS 9.0, *) { 
      searchController?.loadViewIfNeeded() 
     } 

     tableView.delegate = self 
     tableView.dataSource = self 
     resultController.tableView.delegate = self 
    } 
} 

extension: SearchController: UITableViewDataSource { 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return data.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) as? DataCell { 
      return cell.configureCell(data[indexPath.row]) 
     } else { 
      return UITableViewCell() 
     } 
    } 
} 

extension SearchController: UITableViewDelegate { 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if tableView == resultController?.tableView { 
      performSegue(withIdentifier: "DetailView", sender: resultController?.filterData[indexPath.row]) 
     } else { 
      performSegue(withIdentifier: "DetailView", sender: data[indexPath.row]) 
     } 
    } 
} 

extension SearchController: UISearchResultsUpdating, 
    UISearchControllerDelegate, 
    UISearchBarDelegate { 

    func updateSearchResults(for searchController: UISearchController) { 
     let resultsTable = searchController.searchResultsController as! ResultVC 
//  resultsTable.query = searchController.searchBar.text! 
     resultsTable.filterData = data.filter({ 
      $0 == searchController.searchBar.text! 
     }) 
     resultsTable.tableView.reloadData() 
    } 

    func setupSearchControllerWith(_ results: ResultVC) { 
     // Register Cells 
     results.tableView.register(TableCell.self, forCellReuseIdentifier: "DataCell") 
     results.tableView.register(UINib(nibName: "DataCell", bundle: Bundle.main), forCellReuseIdentifier: "DataCell") 

     // We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables. 
     results.tableView.delegate = self 
     searchController = UISearchController(searchResultsController: results)  

     // Set Search Bar 
     searchController.searchResultsUpdater = self 
     searchController.searchBar.sizeToFit() 
     tableView.tableHeaderView = searchController.searchBar 

     // Set delegates 
     searchController.delegate = self 
     searchController.searchBar.delegate = self 
    } 

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     if searchBar.text == nil || searchBar.text == ""{ 
      isSearching = false 
      view.endEditing(true) 
      tableView.reloadData() 
     } else { 
      isSearching = true 
      filterData = data.filter({$0 == searchBar.text}) 
      tableView.reloadData() 
     } 
    } 
} 

その後、ResultsControllerクラス:

class ResultController: UIViewController { 

    @IBOutlet weak var tableView: UITableView! 
    var filterData = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.register(TableCell.self, forCellReuseIdentifier: "DataCell") 
     tableView.register(UINib(nibName: "DataCell", bundle: Bundle.main), forCellReuseIdentifier: "DataCell") 

     tableView.dataSource = self 
    } 
} 

extension ResultController: UITableViewDataSource { 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     let rowCount = filterData.count 
     // When no data insert centered label 
     if rowCount == 0 { 
      blankView(with: textForEmptyLabel) 
     } else { 
      // Remove empty table label 
      tableView.backgroundView = nil 
      tableView.separatorStyle = .singleLine 
     } 
     return rowCount 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) as? DataCell { 
      return cell.configureCell(filterData[indexPath.row]) 
     } else { 
      return UITableViewCell() 
     } 
    } 
} 
+0

ありがとうあなたの助け:) – risa8

関連する問題