Swift Mapkit自动完成



我正在尝试在我的视图控制器中设置一个地址自动完成,以便用户不必键入整个地址,而是从搜索textfield下方选择它。这就是我的控制器的样子:

import Foundation
import UIKit
import MapKit

extension AddNewAddressViewController: MKLocalSearchCompleterDelegate {
    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        searchResults = completer.results
        searchResultsTableView.reloadData()
    }
    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        // handle error
    }
}
class AddNewAddressViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var searchCompleter = MKLocalSearchCompleter()
    var searchResults = [MKLocalSearchCompletion]()

    override func viewDidLoad() {
        searchCompleter.delegate = self
        searchCompleter.queryFragment = addressSearch.text!
        searchResultsTableView.dataSource = self
        super.viewDidLoad()
    }

    @IBOutlet weak var addressSearch: UITextField!
    @IBOutlet weak var searchResultsTableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let searchResultsCount = searchResults.count
        return searchResultsCount
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let searchResult = searchResults[indexPath.row]
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
        cell.textLabel?.text = searchResult.title
        cell.detailTextLabel?.text = searchResult.subtitle
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }

    @IBAction func defaultAddressButton(_ sender: Any) {
    }
    @IBAction func addAddressButton(_ sender: Any) {
    }
}

我遇到的错误说:

类型addNewadDressViewController不符合协议'uitableviewDatasource'

我缺少什么?

预先感谢。

您省略了CellForrowatIndExpath声明的第一个参数的下划线,这是Swift 3.0所需的:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
[etc.]

结果,您没有与预期签名相匹配的必需功能。

最新更新