【问题标题】:How to add select from map option in tableViewCell, Google map, Swift如何在 tableViewCell、Google 地图、Swift 中添加从地图中选择的选项
【发布时间】:2017-12-11 04:31:15
【问题描述】:

我在当前项目中使用谷歌地图。要在我单击 textField 时搜索位置,tableView 会出现在下方,并且我正在使用 GMSAutocompletePrediction 对象填充单元格。到目前为止,它工作正常,到目前为止没有问题。现在我想添加一个像“从地图中选择”这样的单元格,当有人点击一个单元格时,地图中就会出现一个标记。我可以放置标记和附加地图功能,但如何添加最后一个单元格“从地图中选择”?我的代码如下:

 var myFilterPredictions = [GMSAutocompletePrediction]()

然后:

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

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

    tableView.reloadData()
}

填充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

}

我的搜索结果类似于下图: 我想在最后一个单元格中添加“从地图中选择”。

【问题讨论】:

  • 尝试将footer 添加到tableview。此 URL 将有所帮助:stackoverflow.com/questions/38178509/…
  • 不,那不是我想要的,我想要 tableViewCell
  • 你的意思是,你想在tableviewcell中显示地图,对吧?
  • @SagarSukode 不,这不是我想要的,我想添加一个名为“从地图中选择”的单元格,当我点击它时,我将在地图上显示一个标记。我可以显示标记,但我不能将最后一个单元格设置为“从地图中选择”。
  • @ArafinRussell 然后为“从地图中选择”添加一个自定义单元格,并在点击时执行所需的功能(即:didSelectRowAt)。

标签: swift


【解决方案1】:
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
    }
}

您还为此使用 Group 表

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 了吗?
猜你喜欢
  • 1970-01-01
  • 2018-08-01
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多