gpt4 book ai didi

swift - UITextField.text 值不会在 GooglePlaceAutocomplete 上改变

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

我正在使用 Swift 4 在 iOS 11 上开发一个表单应用程序。我想将 Google 选择器 (PlaceAutocomplete) 的返回值放在我的 UITableView 的一个单元格的 UITextField 中。问题是尽管分配了新值,文本字段仍为空白。调试一段时间后,似乎在选择位置时有些东西丢弃了我的 UITextField 的内容。

这些是带有结果值分配的 GooglePlaceAutocomplete 回调:

extension MyUITableViewController, GMSAutocompleteViewControllerDelegate {

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")

let indexPath4 = IndexPath(row: 4, section: 0)
let cell4 = tableView.cellForRow(at: indexPath4) as! InputPlacePicker
cell4.inputTextField.text = place.name

dismiss(animated: true, completion: nil)
}

func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}

func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
}

这是对 GooglePlaceAutocomplete 的调用

@objc func pickPlace() {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}

最佳答案

直接更改单元格可能不是一个好主意,因为每次重新加载 UITableView 时,单元格都会被回收,这取决于您的 tableView(_:cellForRowAt:) 实现,您的 UITextField 可能会被覆盖或最终出现在其他地方。

在您的 View Controller 中拥有某种模型或状态是更好的做法,它保存不同部分中每个单元格的状态。

在您的 GooglePlacesAutocomplete 委托(delegate)中更改此状态(例如地名列表)并在您的 tableView 上调用 reloadData 以从更改后的状态获取新数据。

在您的 tableView(_:cellForRowAt:) 实现中,您在返回之前设置了 inputTextField.text = place.name,这样所有的单元格都应该以来自您内部状态的正确地名。

关于swift - UITextField.text 值不会在 GooglePlaceAutocomplete 上改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48847465/

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