1

我想在我的视图控制器中设置地址自动完成功能,因此用户不必键入整个地址,而是从搜索文本框下方选择它。这是我的控制器看起来像:Swift MapKit自动完成

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”

我缺少什么?

在此先感谢。

回答

1

你忽略了下划线为的cellForRowAtIndexPath宣言,这是根据SWIFT 3.0要求的第一个参数:

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

其结果是,你没有所需的功能相匹配的预期的签名。