如何制作一个函数,当您点击 TableViewController 的行时它 returns 一个特定的坐标?

How to make a function that when you tap a TableViewController's row it returns a specific coordinate?

我正在尝试创建一个应用程序,它有一个带有地图和 9 个地图注释(代表公交车站)的初始视图控制器,它上面还有一个浮动面板 (ContentViewController),它可以向上滑动并且是一个表视图控制器。 TableViewController 列出了所有 9 个公交车站及其名称(每行 1 个游行)。我想要实现的是,当用户点击公交车站名称时,它将 return 所选车站的坐标,这将用于删除所有其他注释并只留下的协议选定的巴士站注释。 还有另一种方法吗?我想使用 if-else 条件,因为它们更简单,但最后一行给了我一个错误。请帮助:(

protocol ContentViewControllerDelegate: AnyObject {
    func selectAnnotation(_ vc: ContentViewController,
                     didSelectLocationWith coordinates: CLLocationCoordinate2D?)
}

...

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myTableView.deselectRow(at: indexPath, animated: true)

        //Only select the pin's coordinates that we want

        func didSelectLocationWith(indexPath: IndexPath) -> (CLLocationCoordinate2D) {
        if indexPath.row == 1 {
            let coordinate =  CLLocationCoordinate2D(latitude: 25.724414,
                                                     longitude: -100.309499)

            
        } else if indexPath.row == 2 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.728990,
                                                    longitude: -100.308499)
            return coordinate
            
        } else if indexPath.row == 3 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.728720,
                                                    longitude: -100.311580)
            return coordinate
            
        } else if indexPath.row == 4 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.729595,
                                                    longitude: -100.313024)
            return coordinate
            
        } else if indexPath.row == 5 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.726635,
                                                    longitude: -100.316835)
            return coordinate
            
        } else if indexPath.row == 6 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.723860,
                                                    longitude: -100.316599)
            return coordinate
            
        } else if indexPath.row == 7 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.723860,
                                                    longitude: -100.313298)
            return coordinate
            
        } else if indexPath.row == 8 {
            let coordinate = CLLocationCoordinate2D(latitude: 25.723800,
                                                    longitude: -100.311735)
            return coordinate
        } else {
            let coordinate = CLLocationCoordinate2D(latitude: 25.723475,
                                                    longitude: -100.310158)
            return coordinate        }
    }
        delegate?.selectAnnotation(self,
        didSelectLocationWith: CLLocationCoordinate2D) //here's the error: Cannot convert value of type 'CLLocationCoordinate2D.Type' to expected argument type     
        
    }

didSelectRowAt 方法中移出 didSelectLocationWith 方法。然后从 didSelectRowAt 调用它来获取坐标并用它调用协议方法。请参阅下面的代码。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  myTableView.deselectRow(at: indexPath, animated: true)
  
  let coordinate = didSelectLocationWith(indexPath: indexPath)
  delegate?.selectAnnotation(self, didSelectLocationWith: coordinate)
}

func didSelectLocationWith(indexPath: IndexPath) -> (CLLocationCoordinate2D) {
  if indexPath.row == 1 {
    return CLLocationCoordinate2D(latitude: 25.724414, longitude: -100.309499)
  } else if indexPath.row == 2 {
    return CLLocationCoordinate2D(latitude: 25.728990, longitude: -100.308499)
  } else if indexPath.row == 3 {
    return CLLocationCoordinate2D(latitude: 25.728720, longitude: -100.311580)
  } else if indexPath.row == 4 {
    return CLLocationCoordinate2D(latitude: 25.729595, longitude: -100.313024)
  } else if indexPath.row == 5 {
    return CLLocationCoordinate2D(latitude: 25.726635, longitude: -100.316835)
  } else if indexPath.row == 6 {
    return CLLocationCoordinate2D(latitude: 25.723860, longitude: -100.316599)
  } else if indexPath.row == 7 {
    return CLLocationCoordinate2D(latitude: 25.723860, longitude: -100.313298)
  } else if indexPath.row == 8 {
    return CLLocationCoordinate2D(latitude: 25.723800, longitude: -100.311735)
  } else {
    return CLLocationCoordinate2D(latitude: 25.723475, longitude: -100.310158)
  }
}