Home > Blockchain >  How to make a function that when you tap a TableViewController's row it returns a specific coor
How to make a function that when you tap a TableViewController's row it returns a specific coor

Time:04-15

I'm trying to create an app with an initial view controller with a map and 9 map annotations (which represent bus stops), it also will have a floating panel above (ContentViewController) that can slide up and is a TableViewController. The TableViewController lists all 9 bus stops with their names (1 parade in each row). What I'm trying to achieve is that when the user clicks on the bus stop name, it will return the coordinates of the selected stop, which will be used on a protocol that removes all the other annotations and only leaves the selected bus stop annotation. Is there another way to do it? I wanted to use if-else conditions because they're simpler, but last line gives me an error. Please help :(

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     
        
    }

CodePudding user response:

Move the didSelectLocationWith method from didSelectRowAt method. Then call it from didSelectRowAt to get the coordinate and call the protocol method with it. See the code below.

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)
  }
}
  • Related