gpt4 book ai didi

Swift MapKit 自动完成

转载 作者:行者123 更新时间:2023-11-28 06:28:16 24 4
gpt4 key购买 nike

我正在尝试在我的 View Controller 中设置一个地址自动完成功能,这样用户就不必键入整个地址,而是从搜索文本字段下方选择它。这是我的 Controller 的样子:

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

}

我收到一条错误消息:

Type AddNewAddressViewController does not conform to protocol 'UITableViewDataSource'

我错过了什么?

提前致谢。

最佳答案

您在 cellForRowAtIndexPath 声明的第一个参数中省略了下划线,这在 swift 3.0 下是必需的:

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

因此,您没有与预期签名匹配的必需函数。

关于Swift MapKit 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com