我正在使用Google自动填充功能将ViewController
中的UITableView填充为UISearchController
。但是,cellForRowAtIndex
并没有被称为断点。
我检查过的内容;
尽管,当我使用UISearchController
实现与主ViewController
相同的代码时,它可以正常工作。可能是因为UISearchController
是一个不同的ViewController
吗?
有什么想法吗?非常感谢您的帮助。-以下代码;
class MapController: UIViewController {
private let resultsController = ResultsController()
private let searchController = UISearchController(searchResultsController: ResultsController())
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
GoogleMapsService.shared.configureMapView(to: self)
GoogleMapsService.shared.googleMapsView.delegate = self
configureNavigationController()
edgesForExtendedLayout = []
definesPresentationContext = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configureLocationManager()
}
func configureNavigationController() {
guard let navigationController = navigationController else { return }
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.barTintColor = .white
navigationItem.titleView = searchController.searchBar
searchController.delegate = resultsController
searchController.searchResultsUpdater = resultsController
searchController.configureSearchController()
}
}
class ResultsController: UIViewController {
private let tableView = UITableView()
private let searchResultsCellIdentifier = "searchResultsCellIdentifier"
private var gmsPlacesClient = GMSPlacesClient()
private var searchResults = [GMSAutocompletePrediction]()
lazy var filteredResults: GMSAutocompleteFilter = {
let filteredResults = GMSAutocompleteFilter()
filteredResults.type = .address
return filteredResults
}()
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
}
func configureTableView() {
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor,
paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0,
width: 0, height: 0)
tableView.tableFooterView = UIView()
tableView.register(ResultsCell.self, forCellReuseIdentifier: searchResultsCellIdentifier)
tableView.keyboardDismissMode = .interactive
tableView.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 243/255, alpha: 1)
}
}
extension ResultsController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let searchResultsCell = tableView.dequeueReusableCell(withIdentifier: searchResultsCellIdentifier, for: indexPath) as! ResultsCell
let result = searchResults[indexPath.row]
searchResultsCell.result = result
return searchResultsCell
}
}
答案 0 :(得分:1)
您正在使用ResultsController
的两个不同实例,因为默认初始化程序ResultsController()
的每次调用都会创建一个新实例。
一种解决方案是延迟声明searchController
。
private lazy var searchController : UISearchController = {
return UISearchController(searchResultsController: resultsController)
}()