gpt4 book ai didi

ios - TableViewCell 中用于打开 map 应用的按钮

转载 作者:行者123 更新时间:2023-11-28 15:47:23 25 4
gpt4 key购买 nike

我有一个tableViewCell,它有下面的代码

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var tr: newTracks!
if inSearchMode {
tr = filteredTrack[indexPath.row]
} else {
tr = items[indexPath.row]
}
let coordinate = CLLocationCoordinate2DMake(tr.lat,tr.lon)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
mapItem.name = tr.name
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey :MKLaunchOptionsDirectionsModeDriving])
}

tr 变量链接到我拥有的结构,该结构收集有关站点的各种数据,包括纬度和经度。这很好用,当我从我的 Tableview 中选择一行时,它会打开苹果 map 并绘制从当前位置到该行位置的路线。

我想做的是在 tableviewcell 上实现一个名为 locate 的按钮,只有当您按下该按钮时,它才会打开苹果 map 并绘制路线,如上所示。

有人可以帮助我了解如何实现按钮以及我的代码需要去哪里吗。

最佳答案

一种方法是使用闭包。这是示例代码。

  1. 在 CustomTableViewCell 中,我们为按钮添加了完成和目标。按下按钮时,将调用完成。

CustomTableViewCell 类:UITableViewCell {

@IBOutlet weak var button: UIButton!

var completion: ((Void) -> Void?)?

override func awakeFromNib() {
button.addTarget(self, action: #selector(didTappedButton), for: .touchUpInside)
}

func didTappedButton() {
if let completion = completion {
completion()
}
}

  1. 在您的 UITableView cellForRow 中,确保您转换为 CustomTableView 并分配完成属性。在 cell.completion 闭包中,您可以调用任何您想要的操作。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
    cell.completion = {
    print("Button tapped")
    }
    return cell
    }

关于ios - TableViewCell 中用于打开 map 应用的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813878/

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