Search code examples
iosswiftswift3eureka-forms

Swift Eureka: Can't dynamically hide/show ButtonRow inside cellUpdate


This is my code for showing and hiding the row. I basically set the hidden attribute as mentioned in the Eureka FAQ. Please let me know if this is the correct way to set the hidden attribute to show/hide the row.

    form
    +++ Section("main")
    <<< ButtonRow () { (row: ButtonRow) -> Void in
        row.tag = "sampleRow"
        if self.shouldHide {
            print("hide exampleRow")
            row.hidden = true
        } else {
            print("show exampleRow")
            row.hidden = false
        }
    }
    .cellSetup ({ [unowned self] (cell, row) in
        row.title = "Title Example"
        row.cell.tintColor = .red
    })
    .cellUpdate({ [unowned self] (cell, row) in
        if self.shouldHide {
            print("cellUpdate: hide exampleRow \(self.shouldHide)")
            row.hidden = true
        } else {
            print("cellUpdate: show exampleRow \(self.shouldHide)")
            row.hidden = false
        }
    })
    .onCellSelection({ (cell, row) in
        print("It's Me!")
    })

Later in the code, I update the variable shouldHide to true or false and call tableView.reloadData(), which does call the cellUpdate block but nothing happens. Could someone please help? Here's my project that you can clone and reproduce this issue. https://github.com/cuongta/testEurekaHideShow

Thanks again!


Solution

  • The correct way to hide a row or even a section in EurekaForm is modifying the .hidden property and call .evaluateHidden() method after that, so to make you code work you need do this modification, your shouldHide var is not required for this task

    replace the .onCellSelection callback method by this one

    .onCellSelection({ (cell, row) in
                        print("It's Me!")
                        row.hidden = true
                        row.evaluateHidden()
                    })
    

    Full Code

        form +++ Section("main")
            <<< ButtonRow () { (row: ButtonRow) -> Void in
                row.tag = "sampleRow"
                }
                .cellSetup ({ (cell, row) in
                    row.title = "Title Example"
                    row.cell.tintColor = .red
                })
                .onCellSelection({ (cell, row) in
                    print("It's Me!")
                    row.hidden = true
                    row.evaluateHidden()
                })
    

    Updated

    If you want to hide a cell from any other context you need to get that cell by tag and after that you can modify .hidden and calling .evaluateHidden() method will be done

    @IBAction func btnAction(_ sender: Any) {
        if let buttonRow = self.form.rowBy(tag: "sampleRow") as? ButtonRow
        {
            buttonRow.hidden = true
            buttonRow.evaluateHidden()
        }
    }