2

I am using google map on my current project. To search places when I click on a textField a tableView appear below and I am populating the cell with GMSAutocompletePrediction object. So far its working fine and no problem so far. Now I want to add a cell like "Select From Map", when someone tap one the cell a marker will appear in the map. I can able to put the marker and additional map feature but how can I add that last cell "Select From Map" ? My code below:

 var myFilterPredictions = [GMSAutocompletePrediction]()

Then:

  func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
    self.myFilterPredictions.removeAll()
    print("GMSAutocompletePrediction *********")

    for i in predictions {
       self.myFilterPredictions.append(i)
    }

    tableView.reloadData()
}

The populating the tableView

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myFilterPredictions.count
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style:.subtitle, reuseIdentifier:"searchCell")

    let predictions = myFilterPredictions[indexPath.row]
    cell.textLabel?.attributedText = predictions.attributedPrimaryText
    cell.detailTextLabel?.attributedText = predictions.attributedFullText
    return cell

}

My search result looks similar to the image below: enter image description here I want to add "Select from map" in the last cell.

Arafin Russell
  • 1,487
  • 1
  • 18
  • 37
  • Try adding `footer` to the `tableview`. This URL will help: https://stackoverflow.com/questions/38178509/swift-add-footer-view-in-uitableview – Amit Dec 11 '17 at 04:34
  • No thats not I want, I want **tableViewCell** – Arafin Russell Dec 12 '17 at 09:30
  • You mean, you want to display map in tableviewcell, right? – Sagar Sukode Dec 13 '17 at 04:55
  • @SagarSukode no no thats not I want, I want to add a cell which will have the name "Select From Map", and when I tap on that I will show a marker on the map. I can show the marker thats fine but I cant set the last cell as "Select from map". – Arafin Russell Dec 13 '17 at 05:01
  • @ArafinRussell Then add one more custom cell for "Select from map" and do your require functionality on tapping (i.e.: didSelectRowAt). – Sagar Sukode Dec 13 '17 at 05:30

1 Answers1

0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
        return myFilterPredictions.count
    }
    else{
        return 1
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    if indexPath.section == 1{
        cell.textLabel = ""Select From Map""
        return cell
    }else{
        cell = UITableViewCell(style:.subtitle, reuseIdentifier:"searchCell")
        let predictions = myFilterPredictions[indexPath.row]
        cell.textLabel?.attributedText = predictions.attributedPrimaryText
        cell.detailTextLabel?.attributedText = predictions.attributedFullText
        return cell
    }
}

You also use Group table for that

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}
Kiran Sarvaiya
  • 1,318
  • 1
  • 13
  • 37