我的目标非常简单,只要有人点击最后一个单元格然后只是添加一个新单元格,但似乎它不起作用。
这里是视图控制器的初始状态
的场景每当有人点击当前或去哪里行时,它都会产生一个谷歌自动完成控制器
假设当前行将被填充,用户必须点击的唯一行是 Where To ,并且一旦用户完成 Where To ,我想添加另一行。
这是&#39>的样子
这是代码
import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController, GMSMapViewDelegate {
// Array for tableView
var locationArray = ["", "Where to?"]
// To be used when user clicks the row
var currentLocationVal = false
var whereToLocationVal = false
@IBOutlet var locationTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Table View stuff
locationTableView.delegate = self
locationTableView.dataSource = self
}
// Google Autocomplete function to spawn the google autocomplete controller, the extension will be at the bottom of the code
func googleAutoCompleteController() {
let googleAutoCompleteController = GMSAutocompleteViewController()
googleAutoCompleteController.delegate = self as GMSAutocompleteViewControllerDelegate
let filter = GMSAutocompleteFilter()
filter.type = .establishment
googleAutoCompleteController.autocompleteFilter = filter
present(googleAutoCompleteController, animated: true, completion: nil)
}
// This is the function that will add the new row/cell
func addedPromoTableCells() {
locationArray.append("Promo")
let indexPath = IndexPath(row: locationArray.count - 1, section: 0)
DispatchQueue.main.async {
self.locationTableView.beginUpdates()
self.locationTableView.insertRows(at: [indexPath], with: .automatic)
self.locationTableView.endUpdates()
}
}
addedPromoTableCells()
函数是我想要运行的代码
在ViewController上扩展tableView委托(不要让代码太长)
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locationArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GetLocation", for: indexPath)
let location = locationArray[indexPath.row]
if let locationCell = cell as? GetLocationTableViewCell {
locationCell.locationTitle.text = location
}
return cell
}
// table view delegate function that will spawn the google autocomplete controller when user select one of the rows.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.row {
case 0:
let selected = indexPath.row
if (selected == 0) {
currentLocationVal = true
googleAutoCompleteController()
}
case 1:
let selected = indexPath.row
if (selected == 1) {
whereToLocationVal = true
googleAutoCompleteController()
}
default :
print("Number out of range")
}
}
}
Google自动填充代码扩展程序
extension ViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
// First cell
let firstLocationCell = locationTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? GetLocationTableViewCell
// Second Cell
let secondLocationCell = locationTableView.cellForRow(at: IndexPath(row: 1, section: 0)) as? GetLocationTableViewCell
// if currentLocationVal is true then update the first cell
if (currentLocationVal == true) {
firstLocationCell?.locationTitle.text = place.name
currentLocationVal = false
// if whereToVal is true then update the second cell
} else if (whereToLocationVal == true) {
secondLocationCell?.locationTitle.text = place.name
addedPromoTableCells() // This is where the line supposed to be executed
whereToLocationVal = false
}
dismiss(animated: true, completion: nil)
}
// There are more codes here, but don'w want to complicate things because it is not relevant to the use case.
}
因此,只要第二行完成,表格视图就不会更新。