单元格未出现在UITableView中-Swift 3

z头

我有以下代码,该代码旨在接收来自a的用户输入UISearchBarMKLocalSearch基于它创建并启动a ,然后在中显示所有MKMapItem的名称UITableView以下代码不起作用,而是因错误而崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

代码:

import UIKit
import MapKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

    var localSearchRequest:MKLocalSearchRequest!
    var localSearch:MKLocalSearch!
    var localSearchResponse:MKLocalSearchResponse!
    var locations = [MKMapItem]()

    @available(iOS 8.0, *)
    public func updateSearchResults(for searchController: UISearchController) {
        localSearch?.cancel()
        localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = searchController.searchBar.text
        localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.start { (localSearchResponse, error) -> Void in
            if let response = localSearchResponse {
                self.locations.removeAll()
                for mapItem in response.mapItems {
                    self.locations.append(mapItem)
                    print(mapItem)
                }
                if !response.mapItems.isEmpty {
                    let indexPaths = (0..<response.mapItems.count).map { IndexPath(row: $0, section: 0) }
                    self.tableView.insertRows(at: indexPaths, with: .none)
                }
            }
        }
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.locations.count;
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
        let row = indexPath.row
        cell.textLabel?.text = self.locations[row].name
        return cell
    }

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.sizeToFit()
        searchController.searchBar.tintColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 1)
        searchController.searchBar.placeholder = "Location"
        searchController.searchBar.returnKeyType = UIReturnKeyType(rawValue: 6)!
        self.tableView.tableHeaderView = searchController.searchBar
    }

    override func viewWillDisappear(_ animated: Bool) {
        searchController.isActive = false
        searchController.searchBar.isHidden = true
    }

    override func viewDidDisappear(_ animated: Bool) {
        searchController.isActive = false
        searchController.searchBar.isHidden = true
    }
}


TableViewController插座:

TableViewController插座

如何在您的viewDidLoad中添加这一行?

tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章