cellForRowAtIndex没有被调用

时间:2018-12-31 15:18:11

标签: ios swift uitableview google-places-api googleplacesautocomplete

我正在使用Google自动填充功能将ViewController中的UITableView填充为UISearchController。但是,cellForRowAtIndex并没有被称为断点。

我检查过的内容;

  • 数据源设置为自我
  • 将代理设置为自我
  • 正在控制台中打印数据
  • 我拥有所有委托和数据源方法

尽管,当我使用UISearchController实现与主ViewController相同的代码时,它可以正常工作。可能是因为UISearchController是一个不同的ViewController吗?

有什么想法吗?非常感谢您的帮助。-以下代码;

MapController

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()
    }
}

ResultsController

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
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用ResultsController的两个不同实例,因为默认初始化程序ResultsController()的每次调用都会创建一个新实例。

一种解决方案是延迟声明searchController

private lazy var searchController : UISearchController = { 
    return UISearchController(searchResultsController: resultsController) 
}()