使用另一个视图控制器(带有表视图)来显示UISearchController的结果

时间:2018-04-25 13:54:20

标签: ios swift uitableview uiviewcontroller uisearchbar

我有两个视图控制器。第一个(CityViewController)包含一个搜索栏,因此在那里完成配置。第二个(CitySearchViewController)用于显示搜索结果(我需要自定义单元格,这就是我不仅仅使用UITableViewController)的原因。以下是在第一个视图控制器中配置search controller的代码:

class CityViewController: UIViewController {

var searchController: UISearchController?

var citySearchViewController: CitySearchViewController?

override func viewDidLoad() {
    super.viewDidLoad()


    citySearchViewController = CitySearchViewController()
    searchController = UISearchController(searchResultsController: citySearchViewController)

    //Setting the search controller
    setSearchController()
}


//Setting the search controller
private func setSearchController() {
    if let searchController = searchController {
        searchController.searchResultsUpdater = citySearchViewController
        searchController.obscuresBackgroundDuringPresentation = true
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.placeholder = "Search for a city"
        navigationItem.searchController = searchController
        definesPresentationContext = true
        searchController.searchBar.returnKeyType = .done
        //searchController.searchBar.enablesReturnKeyAutomatically = false
        searchController.searchBar.delegate = self

    }
   } 
}



extension CityViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchController?.isActive = false
   } 
} 

因此,我将第二个视图控制器(CitySearchViewController)设置为searchResultsController。在那里我有一个城市数组,视图控制器是数据源和表视图的委托,它在里面。

以下是视图控制器内的完整代码。

class CitySearchViewController: UIViewController {

@IBOutlet weak var tableView: UITableView! {
    didSet {
        tableView.delegate = self
        tableView.dataSource = self
    }
}


  let cities = [City(name: "Saint Petersburg", coordinates: CLLocation(latitude: 59.93863, longitude: 30.31413)), City(name: "Moscow", coordinates: CLLocation(latitude: 55.75222, longitude: 37.61556)), City(name: "London", coordinates: CLLocation(latitude: 51.509865, longitude: -0.118092)), City(name: "Chicago", coordinates: CLLocation(latitude: 41.881832, longitude: -87.623177)), City(name: "Sochi", coordinates: CLLocation(latitude: 43.588348, longitude: 39.729996)), City(name: "Madrid", coordinates: CLLocation(latitude: 40.416775, longitude: -3.703790)), City(name: "New York", coordinates: CLLocation(latitude: 40.730610, longitude: -73.935242)), City(name: "Paris", coordinates: CLLocation(latitude: 48.864716, longitude: 2.349014)), City(name: "Amsterdam", coordinates: CLLocation(latitude: 52.370216, longitude: 4.895168)), City(name: "Barcelona", coordinates: CLLocation(latitude: 41.390205, longitude: 2.154007))]
var filteredCities = [City]()



override func viewDidLoad() {
    super.viewDidLoad()

}



private func filterContentForSearchText(_ searchText: String) {
    filteredCities = cities.filter({ (city) -> Bool in
        return city.name.lowercased().contains(searchText.lowercased())
    })
    tableView.reloadData()

 } 

extension CitySearchViewController: UITableViewDataSource {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "citySearchCell", for: indexPath) as! CitySearchTableViewCell
    let filteredCity = filteredCities[indexPath.row]
    cell.cityLabel.text = filteredCity.name

    return cell
}

 }

extension CitySearchViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
  }
 }

 extension CitySearchViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    filterContentForSearchText(searchController.searchBar.text!)
 }
}

我在那里进行基本的表视图设置,并且还过滤数组并相应地更新表视图。

问题是,一旦我开始在搜索栏中点击一个单词,我就会得到一个例外。原因是CitySearchViewController内的表格视图为nil。所以,我认为,当我从CityViewController内的这行代码初始化视图控制器时,我做错了什么:

override func viewDidLoad() {
super.viewDidLoad()


citySearchViewController = CitySearchViewController()
searchController = UISearchController(searchResultsController: citySearchViewController)

//Setting the search controller
setSearchController()
}

我还尝试从故事板中实例化视图控制器,但是,在这种情况下,我收到一个错误,即在initiaziztion期间没有使用storyboardId的视图控制器。我检查并重新检查了视图控制器的id,它与代码中使用的完全相同。以下是我尝试从第一个CitySearchViewController实例化CityViewController的初始方式。

override func viewDidLoad() {
    super.viewDidLoad()

    citySearchViewController = UIStoryboard().instantiateViewController(withIdentifier: "searchCityTableView") as? CitySearchViewController

    //Setting the search controller
    setSearchController()
}

我没有使用其他视图控制器来显示搜索控制器的结果,所以我想我做错了。

如果您知道我做错了什么或知道正确的做法,我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

问题#1

如果您通过以下方式初始化CitySearchViewController

citySearchViewController = CitySearchViewController()

这意味着它会跳过您在Interface Builder中为该视图控制器设置的故事板中设置的任何内容 - 最重要的是,IBOutlet到您的tableView

@IBOutlet weak var tableView: UITableView!

问题#2

您还指出您尝试使用此行代码将CitySearchViewController实例化,并显示错误,指出找不到具有该标识符的VC:

citySearchViewController = UIStoryboard().instantiateViewController(withIdentifier: "searchCityTableView") as? CitySearchViewController

由于您没有指定故事板的名称来实例化VC,因此可能是错误的。您需要将故事板的名称指定为:

citySearchViewController = UIStoryboard(name: "<storyboard filename here>", bundle: nil).instantiateViewController(withIdentifier: "searchCityTableView") as? CitySearchViewController

(其中<storyboard filename here>类似于“搜索”,名为“Search.storyboard”的故事板。