Search code examples
swifteureka-forms

Eureka Swift - set value of LocationRow


I've got a form where a user can pick from a number of objects that includes co-ordinate data, which I want to load into a LocationRow.

I have tried several different ways to try and set the Location Row value, but it either crashes (unexpectedly found nil while unwrapping an optional Value) or doesn't reload the table with the correct data. i.e. https://i.sstatic.net/ivW9r.png

My LocationRow eureka code:

$0.rowRight = LocationRow(){
                    $0.title = "Location"
                    $0.tag = "location"
                    if let page = selectedPage {
                        if let pageLocationLatitude = page.locationLatitude.value,
                            let pageLocationLongutude = page.locationLongitude.value {
                            print("testing for row update")
                            $0.value = CLLocation(latitude: pageLocationLatitude, longitude: pageLocationLongutude)
                        }
                    }

                }

and the function that is called when I want to update the LocationRow

 private func setSelectedPage(pageName : String) {
        print("setting location of page: \(pageName)")
        if pageName == username {
            return
        }
        selectedPage = userPages?.filter("name == \"\(pageName)\"").first
        if let locationLongitude = selectedPage?.locationLongitude.value,
            let locationLatitude = selectedPage?.locationLatitude.value {

        print("lat and lon: \(locationLatitude) \(locationLongitude)")

        /*PURELY FOR TESTING
        let titleRow = self.form.rowBy(tag: "title") as! TextRow
        titleRow.value = "TEST WORKS OK"
        titleRow.updateCell()
        PURELY FOR TESTING */

        let locationRow = self.form.rowBy(tag: "location") as! LocationRow
        locationRow.value = CLLocation(latitude: locationLatitude, longitude: locationLongitude)
        self.form.rowBy(tag: "location")?.updateCell()
    }


   self.tableView.reloadData()
}

Solution

  • From your code, I can see that you are putting this location row in a SplitRow:

    // rowRight is a property of SplitRow
    $0.rowRight = LocationRow(){
    

    Therefore, the form doesn't really know about the location row. It only knows about the split row. You should get the split row using its tag first, then access rowRight.

    // use the tag of your split row here!
    let splitRow = self.form.rowBy(tag: "splitRow") 
                        as! SplitRow<SomeRow, LocationRow> // replace SomeRow with the type of row on the left of the split row
    let locationRow = splitRow.rowRight
    locationRow.value = CLLocation(latitude: locationLatitude, longitude: locationLongitude)
    locationRow.updateCell()