细胞没有出现在UITableView - Swift 3中

时间:2017-01-29 07:47:57

标签: ios swift uitableview swift3 mapkit

我有以下代码,用于从UISearchBar获取用户输入,根据它创建并启动MKLocalSearch,然后显示所有MKMapItem&# 39; 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 Outlets:

TableViewController Outlets

2 个答案:

答案 0 :(得分:2)

如何在viewDidLoad中添加此行?

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

答案 1 :(得分:0)

原来我没有设置正确的dataSource类来链接到UITableView 注册单元格原型.xib也允许我在插入具有重用标识符的单元格时不会出错[/ p>