Eureka: Dynamically Creating/Adding TextRows in Section

Created on 28 Jun 2016  路  6Comments  路  Source: xmartlabs/Eureka

Please refer to the following code:

+++ Section()
            <<< SegmentedRow<String>("segments"){
                $0.options = ["Assets", "Notes", "Photos"]
                $0.value = "Assets"
            }
            +++ Section(){
                $0.tag = "assets_s"
                $0.hidden = "$segments != 'Assets'" // .Predicate(NSPredicate(format: "$segments != 'Sport'"))
            }
            for t in myarray{
                <<< TextRow(){
                $0.title = "Which is your favourite soccer player?"
            }

            }

I am trying to create or add TextRows depending on the number of contents of my array. What is the correct way of doing this?

Thank you!

question

Most helpful comment

I do not get your problem, what you are doing is correct.
Of what type is your array? You should have an array of structs or whatever that hold all the information needed for each row.
To append those rows you will have to create the section like:

let section = Section() { ... }
for t in myarray {
    section <<< TextRow() { ... }
}

All 6 comments

I do not get your problem, what you are doing is correct.
Of what type is your array? You should have an array of structs or whatever that hold all the information needed for each row.
To append those rows you will have to create the section like:

let section = Section() { ... }
for t in myarray {
    section <<< TextRow() { ... }
}

I have the same issue.

            Section("Detail Location Filter")

            <<< CheckRow() { row in
                row.title = user?.location.city
                row.value = false
            }

            for location in countryArray {
                <<< CheckRow() { row in
                    row.value = false
                    row.title = String(location)
                }
            }

But I get the error: <<< is not a prefix unary operator.

The <<< operator needs a form or section in front.

You should do it as in the example I wrote above.

Similar issue as well. I am getting the error:

Binary operator '+++' cannot be applied to operands of type '()' and 'Section'

The error is showing up on the next section you try to create after using the example you listed above. I am using the Swift 4.2 branch which may make a difference.

@antonijap & @riverbaymark
I had a similar issue, and after reading this thread (and still being confused.) I figured out there was a better way to dynamically add rows to a Section for an Eureka form.

The technique I found that works more reliably, and lets you target sections in a more dynamic way, is to use the self.form.sectionBy(tag: <String>). This lets you target any Section() you've added a $0.tag field too.

Here's a generic example of my solution that I hope helps others that found the previous solution a bit confusing.

override func viewDidLoad() {
        super.viewDidLoad()

        let numberRows = 3 // This defines the number of rows that will be added to a section in the `for loop` below.

        form
            +++ Section("Welcome to the form!") {
                $0.tag = "firstSection"
            }

            +++ Section("Please answer these questions.") {
                $0.tag = "secondSection"  // This tag defines the `Section` that rows will be added too.
            }

            for aRow in 1 ... numberRows {
                // The trick here is to use the `self.form.sectionBy(tag: "")! <<<` then add your row type.
                // You can add any row types after the <<< just as you normally would in a standard Eureka form.
                // The tag is the important part. This means you can add rows to any section that you have tagged.
                self.form.sectionBy(tag: "secondSection")! <<< CheckRow() {
                    $0.title = "\(aRow). Did this work?"
                    $0.value = false
                }
            }
}

@antonijap & @riverbaymark & @kenjikato
I did it the following way to be independent from string matchings by tag values:

let section = Section()
form +++
    section
    myArray.forEach {
        section <<< TextRow($0) {
            $0.title = $0.tag
        }
    }
Was this page helpful?
0 / 5 - 0 ratings