iosswiftswift2eureka-forms

Dynamically loading options for a row


This is a question related to the Swift open source project called Eureka and I'm posting this question here on SO since they monitor their tag here.

Okay so I create a form in the viewDidLoad method of a view controller. At the same time I make a http call to an API to get a list of data (jobs). This call is asynchronous. I want to display these jobs as option values in a PushRow.

Since this API call is asynchronous, by the time this data is received, the form is already set up. I have a class level array setup to be populated with the list of jobs from the http call. That same array is assigned to that row's options property. I assign the values to the array when I receive the data. But then when I go and tap on the PushRow, it's empty.

class CreateEventViewController: FormViewController {

    private var jobs = [Job]()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupForm()

        api.getJobs(.LeadCreated) { jobs, error in
            if let error = error {
                print(error.localizedDescription)
                return
            }

            if let jobs = jobs {
                self.jobs = jobs
            }
        }
    }

    private func setupForm() {
        form
            +++ Section()
            <<< PushRow<String>("Jobs") {
                $0.title = "Choose Job"
                $0.options = jobs.map { $0.description }
                $0.selectorTitle = "Choose..."
            }.onPresent { from, to in
                to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: from, action: #selector(CreateEventViewController.multipleSelectorDone(_:)))
            }
    }

}

Is there some sort of a way to refresh or reload the data for a specific row or the form as a whole?


Update 1

As per Kevin's answer below, I modified my code as below.

override func viewDidLoad() {
    super.viewDidLoad()

    api.getJobs(.LeadCreated) { jobs, error in
        if let error = error {
            print(error.localizedDescription)
            return
        }

        if let jobs = jobs {
            self.jobs = jobs

            if let jobsRow = self.form.rowByTag("Jobs") {
                jobsRow.updateCell()
            }
        }
    }

    setupForm()
}

But for some reason, it's still not working. The options are empty. The cell is correctly picked up via the tag, I checked. Anyway failing this, I also tried tableView.reloadData() as well but to no avail.


Solution

  • I found it! You had to add the .cellUpdate {...} closure to the row and inside it assign the values to the options property. Like so,

    <<< PushRow<String>("Jobs") {
        $0.title = "Choose Job"
        $0.options = jobs.map { $0.description }
        $0.selectorTitle = "Choose..."
    }
    .cellUpdate { cell, row in
        row.options = self.jobs.map { $0.description }
    }