Eureka icon indicating copy to clipboard operation
Eureka copied to clipboard

Bug When I leave my forms and return several times strange spacing are created

Open mickaelhero opened this issue 5 years ago • 2 comments

ezgif com-video-to-gif

Hello a strange bug occurs when I enter several times in my controller form. Only the first time I enter the controller everything is displayed correctly.

There is a reset function here is my code

Controller:

override func viewDidLoad() {
        super.viewDidLoad()
       viewModel.addForm(in: self, with: term)
    }

ViewModel:

class AddNewFilterViewModel {
    let firstSection = Section(header: "Keyword", footer: "The keyword will be searched on your message body or sender to detect whether it is spam or not.")
    let secondSection = Section("Rules")
    let thirdSection = Section()

    let keywordRow = TextRow() { row in
        row.placeholder = "Enter keyword.."
    }

    lazy var deleteRow = ButtonRow { row in
        row.title = "Delete filter"
    }.cellUpdate { cell, row in
        cell.textLabel?.textColor = .red
    }.onCellSelection { _, _ in
        self.delegate?.deleteRowClicked()
    }

    let segmentedRow = SegmentedRow<String>() { row in
        let opts = Term.Option.allCases.map { $0.name }
        row.title = "Appears in"
        row.options = opts
    }.cellUpdate { cell, row in
        cell.segmentedControl.selectedSegmentIndex = 0
    }

func addForm(in viewController: AddNewFilterViewController, with term: Term?) {
        if let term = term {
            keywordRow.cellUpdate { cell, row in
                cell.textField.text = term.value
            }

            segmentedRow.cellUpdate { cell, row in
                cell.segmentedControl.selectedSegmentIndex = term.option.rawValue
            }
        }

        firstSection.append(keywordRow)
        secondSection.append(segmentedRow)

        viewController.form.append(firstSection)
        viewController.form.append(secondSection)

        if term != nil {
            thirdSection.append(deleteRow)
            viewController.form.append(thirdSection)
        }
    }
}

mickaelhero avatar Dec 06 '19 09:12 mickaelhero

Are you recreating the form each time? Or reusing the same view controller?

mats-claassen avatar Dec 10 '19 19:12 mats-claassen

Because the viewModel is singleton, every time I open the controller ca add the rows. I just dump the sections in the addForm methods:

func addForm(in viewController: AddNewFilterViewController, with term: Term?) {
    firstSection.removeAll()
    secondSection.removeAll()
    thirdSection.removeAll()

        if let term = term {
            keywordRow.cellUpdate { cell, row in
                cell.textField.text = term.value
            }

            segmentedRow.cellUpdate { cell, row in
                cell.segmentedControl.selectedSegmentIndex = term.option.rawValue
            }
        }

        firstSection.append(keywordRow)
        secondSection.append(segmentedRow)

        viewController.form.append(firstSection)
        viewController.form.append(secondSection)

        if term != nil {
            thirdSection.append(deleteRow)
            viewController.form.append(thirdSection)
        }
    }

mickaelhero avatar Dec 11 '19 07:12 mickaelhero