xmartlabs / Eureka

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

I'm using a segmented control to hide or show rows and I keep getting an error. #2217

Closed ClearlyAwesome closed 2 years ago

ClearlyAwesome commented 2 years ago

I'm using a segmented control to hide or show rows and I keep getting an error.

The error: Eureka/Section.swift:247: Fatal error: Section: Index out of bounds.

The code:

func drop() {
        global.communityTitle = "Posting"
        global.containerHeight.constant = screenSize.height/1.4
        tableView.isScrollEnabled = true
        form = Section()

        <<< SegmentedRow<String>("type"){
            $0.options = ["Hiring", "Looking for work"]
            $0.value = "Hiring"
        } .onChange { [unowned self] (row) in
            tableView.reloadData()
        }
        +++ Section(){
            $0.tag = "Hiring"
            $0.hidden = "$type != 'Hiring'"
    }
        <<< TextRow(){
            $0.title = "Job Title"
            $0.placeholder = "Enter text here"
            $0.add(rule: RuleRequired())
            $0.validationOptions = .validatesOnChangeAfterBlurred
            $0.hidden = "$type != 'Hiring'"
        } .cellUpdate { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .systemRed
            }
        }
        +++ Section(){
            $0.tag = "Looking for work"
            $0.hidden = "$type != 'Looking for work'"
        }
        <<< TextRow("Posting Title"){
            $0.title = $0.tag
            $0.placeholder = "Enter text here"
            $0.add(rule: RuleRequired())
            $0.validationOptions = .validatesOnChangeAfterBlurred
        } .cellUpdate { cell, row in
            if !row.isValid {
                cell.titleLabel?.textColor = .systemRed
            }
        }

I think it is either of both of these lines of code:

        global.containerHeight.constant = screenSize.height/1.4
        tableView.isScrollEnabled = true

Every time I remove them, the code works. But I want them in the code. Any ideas why this is crashing my app?

mats-claassen commented 2 years ago

Hey @ClearlyAwesome have you been able to solve this? Do you need the tableView.reloadData() to change the rows that are shown in the form?

ClearlyAwesome commented 2 years ago

I figure it out by creating a separate class for each view I wanted to load. tableView.reloadData() kept crashing my app for some reason. I think the forms had different number of rows....? no clue. it is working now that I have creating them in completely different classes.

mats-claassen commented 2 years ago

OK, so I will close this issue then.