簡體   English   中英

如何實現自動完成表格視圖的Google Maps Swift?

[英]How do I implement autocomplete table view google maps swift?

我正在嘗試為城市實施Google地圖自動填充功能。 當他們在文本字段中鍵入內容時,表格視圖應根據用戶的輸入自動填充潛在的城市。 現在,我想在日志中打印結果,但是我不確定是否正確執行了此操作。 這是顯示在我的日志中的Result Austin{ GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x608000235e60>"; } Result Austin{ GMSAutocompleteMatch = "<GMSAutocompleteMatchFragment: 0x608000235e60>"; }

var placesClient: GMSPlacesClient!

override func viewDidLoad() {
    super.viewDidLoad()

    placesClient = GMSPlacesClient.shared()

}



func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        placeAutoComplete()
        return true
    }

    func placeAutoComplete() {
        let filter = GMSAutocompleteFilter()
        filter.type = .city
        placesClient.autocompleteQuery(cityField.text!, bounds: nil, filter: filter, callback: {(results, error) -> Void in
            if let error = error {
                print("Autocomplete error \(error)")
                return
            }
            if let results = results {
                for result in results {
                    print("Result \(result.attributedPrimaryText)")
                }
            }
        })

    }

您正在打印屬性字符串。 UILableattributedText屬性中顯示它時,它不應出現。 請參閱下面的示例

當您result in results調用result in results保存在數組中,並通過調用tableView.reloadData重新加載tableview。 在tableView數據源中,將行數設置為數組中的項目數。 然后在cellForRowAtIndexPath內部執行以下操作

let result = resultArray[indexPath.row] as? GMSAutocompletePrediction
cell.textLabel.attributedText = result.attributedPrimaryText

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM