xmartlabs / Eureka

Elegant iOS form builder in Swift
https://eurekacommunity.github.io
MIT License
11.78k stars 1.33k forks source link

Reordering sections and rows will return the same initial order and not the updated ones #2239

Open DigitalVanilla opened 1 year ago

DigitalVanilla commented 1 year ago

Hello

I needed to rearrange sections and rows programmatically and with drag'/drop functionality, but I did hit a small problem when I want to cycle again the form to get the data in the order after operations like move(fromOffsets: [0], toOffset: 2), but Im getting the same initial order and not the updated one.

My code:

form

    +++ Section() { section in
      section.tag = "anchor_1"
    }

    <<< EKIconTitleExtraRow() { row in
      row.tag = "row_1"
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 1"
      row.value = "extra stuff"
    }

    <<< EKIconTitleExtraRow() { row in
      row.tag = "row_2"
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 2"
      row.value = "extra stuff"
    }

    <<< EKIconTitleExtraRow() { row in
      row.tag = "row_3"
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 3"
      row.value = "extra stuff"
    }

    +++ Section() { section in
      section.tag = "anchor_2"
    }

    <<< EKIconTitleExtraRow() { row in
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 4"
      row.value = "extra stuff"
    }

    <<< EKIconTitleExtraRow() { row in
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 5"
      row.value = "extra stuff"
    }

    <<< EKIconTitleExtraRow() { row in
      row.icon = UIImage(named: "meal_details_icon")
      row.title = "Some row to move 6"
      row.value = "extra stuff"
    }

then I move the first section below the second like:

form.move(fromOffsets: [0], toOffset: 2)

then I wish to see the order of the section tags:

print(self.form.allSections.map({ section in return section.tag }))

but Im getting always ["anchor_1", "anchor_2"] where I was expecting ["anchor_2", "anchor_1"]; and this is the same for the rows that I try to move using the native drag delegate, where visually I see the change in the order after the drop but if I cycle the section rows with allRows I get the same initial order. What am I missing?

Thanks

mats-claassen commented 1 year ago

Hi, have you tried using Multivalued Sections? There is an example in the example project and in the README. That is the case for which Eureka has reordering implemented.

If that doesn't suit your needs you could override these delegate functions in your FormViewController subclass:

    open func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
    open func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath
    open func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)

They are implemented in Core.swift from line 873, if you need some guidance

DigitalVanilla commented 1 year ago

Yeah, unfortunately I cannot use the Multivalued Sections because we need to move sections itself using a single row as "fake header" to rearrange; unfortunately Apple never gave us the ability to actually drag'n drop an entire section