Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var locations = [Location]()
- var searchController:UISearchController!
- var searchResults = [Location]()
- override func viewDidLoad() {
- super.viewDidLoad()
- searchController = UISearchController(searchResultsController: nil)
- tableView.tableHeaderView = searchController.searchBar
- searchController.searchResultsUpdater = self
- searchController.dimsBackgroundDuringPresentation = false
- self.tableView.reloadData()
- }
- func filterContent(for searchText: String) {
- searchResults = locations.filter({ (location) -> Bool in
- if let name = location.name {
- let isMatch = name.localizedCaseInsensitiveContains(searchText)
- return isMatch
- }
- return false
- })
- }
- func updateSearchResults(for searchController: UISearchController) {
- if let searchText = searchController.searchBar.text {
- filterContent(for: searchText)
- tableView.reloadData()
- }
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if searchController != nil && searchController.isActive {
- return searchResults.count
- } else {
- return locations.count
- }
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cellIdentifier = "cell"
- let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! LocationTableViewCell
- // The following line is where I receive the error, as I attempt to determine whether the app should get the result from the filtered search result
- let location = (searchController.isActive) ? searchResults[indexPath.row] : locations[indexPath.row]
- cell.nameLabel.text = locations[indexPath.row].name
- cell.thumbnailImageView.image = UIImage(named: locations[indexPath.row].image)
- cell.locationLabel.text = locations[indexPath.row].location
- cell.typeLabel.text = locations[indexPath.row].type
- cell.accessoryType = locations[indexPath.row].isVisited ? .checkmark : .none
- return cell
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement