import UIKit
import MapKit
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, CLLocationManagerDelegate {
private var tableView: UITableView!
var filteredOffices = [OfficeStats]()
var filteredNames = [String]()
lazy var VC = ViewController()
let searchBar = UISearchBar()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setUpNavBar()
searchBar.delegate = self
filteredOffices = VC.officesCopy
VC.retrieveNameCopy() { (success, filteredOffices) in
if success {
self.VC.sortList()
} else {
print("unsuceess")
}
}
let barHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
tableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
tableView.rowHeight = UITableView.automaticDimension
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = ""
searchBar.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredOffices = []
if searchText == "" {
filteredOffices = VC.officesCopy
}
else{
for offices in VC.officesCopy {
if offices.name.lowercased().contains(searchText.lowercased()){
filteredOffices.append(offices)
}
}
}
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(filteredOffices.count)
return filteredOffices.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dismiss(animated: true)
print(filteredOffices[indexPath.count])
let geoPoint = filteredOffices[indexPath.count].coordinates
let location = CLLocationCoordinate2D(latitude: geoPoint.latitude, longitude: geoPoint.longitude)
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
self.VC.LiveMap.setRegion(region, animated: true)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchBar.becomeFirstResponder()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
cell.textLabel?.text = filteredOffices[indexPath.row].name
cell.detailTextLabel?.text = filteredOffices[indexPath.row].location
return cell
}
func setUpNavBar() {
searchBar.sizeToFit()
searchBar.searchBarStyle = .prominent
searchBar.placeholder = "Kërko"
searchBar.tintColor = UIColor.lightGray
searchBar.barTintColor = UIColor.lightGray
navigationItem.titleView = searchBar
searchBar.isTranslucent = true
}
}
I have incorporated a search bar in a map that displays different results of specific pins that are placed on the map. I am trying to redirect the user to a specific pin when he taps a table view cell. Unfortunately I get this error:
"Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
on the line:
self.VC.LiveMap.setRegion(region, animated: true)