问题描述
我在地图中加入了一个搜索栏,可以显示放置在地图上的特定图钉的不同结果。当他点击表格视图单元格时,我试图将用户重定向到特定的引脚。不幸的是,我收到此错误:
“线程 1:致命错误:在隐式解包可选值时意外发现 nil”
上线:
self.VC.LiveMap.setRegion(region,animated: true)
代码:
import UIKit
import MapKit
class SearchViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,CLLocationManagerDelegate {
private var tableView: UITableView!
var filteredOffices = [OfficeStats]()
var filterednames = [String]()
var VC = ViewController()
let searchBar = UISearchBar()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
sortList()
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
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)