简体   繁体   中英

How can I update my list in Eureka when the segment is changing?

I made a form with a checkrow list and two segments where the user can either choose to select all items in the list or none.

 private func setupForm() -> Void {
        
        guard let inheritorList = getInheritorList() else {
            return
        }

        
        form +++ Section()
            
            <<< LabelRow() { row in
                row.title = "Choose between multiple inheritors"
            }
            
            <<< SegmentedRow<Int>("segments") { row in
                row.options = [1,2]
                row.displayValueFor = { idx in
                    if idx == 1 {
                        return "All"
                    } else {
                        return "None"
                    }
                }
            }
            .onChange({ row in
                if row.value! == 1 {
                    //All checkboxes should be selected
                    self.inheritorFilterMode = .filterAll
                } else {
                    //All checkboxes should be deselected
                    self.inheritorFilterMode = .filterNone
                }
            })
        
        for inheritor in inheritorList {
                    form.last! <<< CheckRow("inheritors") { listRow in
                        listRow.title = inheritor.name
                        listRow.value = nil
                    }
                    .onChange({ row in
                        if row.value! {
                            self.inheritorsArray.insert(row.tag!)
                        } else {
                            self.inheritorsArray.remove(row.tag!)
                        }
                        
                    })
        }
    }
    

This creates a list with the above specified inheritors list and two segments. My problem is that I don't know how to obtain the values of the segmented row.

I tried to create an enum and change the filter mode depending on the value in the onChange of my segmented row method. I would then later on in the onChange of my check row list check which mode is active but this didn't work.

enum InheritorFilterMode {
    case filterAll, filterNone
}
...

var inheritorFilterMode: InheritorFilterMode = .filterAll

I also tried to obtain the value of the segments by getting the tag:

let segmentedRow: SegmentedRow<Int>! = self.form.rowBy(tag: "segments")

but this approach didn't work as well.

Any suggestions on how to update the check boxes when the segment changes?

I'm not quite sure if i understood the question. But I ll try to answer it.

The checkmark itself is just a accessory type which changes the value of the row (check the repo for reference:

 accessoryType = row.value == true ? .checkmark : .none

So instead of filtering the results (which shouldn't work the way you did it) you should loop all rows in your form. There is a method form.allRows() which allows you to loop over all rows in your form and then you only need to check which segment - in your case "All" or "None" - is selected:

So in the onChange block of your SegmentedRow you could do something like:

.onChange({ segmentedRow in
   for row in self.form.allRows {
      if row is CheckRow {
        if segmentedRow.value == "All" {
           row.baseValue = true
        } else if segmentedRow.value == "None" {
            row.baseValue = false
          }
          // don't forget to call update cell - otherwise the cell won't update
          row.updateCell()
      }
   }
})

Also: Instead of using an Interger based row type you could just use a String :

<<< SegmentedRow<String>("yourTagHere") { (segment) in 
    segment.options = ["All","None"]
...

I didn´t test it myself but it should do the trick. It took me 10 minutes to read through the Eureka docs on GitHub. Might be worth a try ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM