Search code examples
iosswifteureka-forms

iOS Eureka Change section header after creation


I am reloading data in a Eureka form in viewDidAppear method:

i have something like:

in viewDidLoad()

form +++ Section("contacts-selected")

in viewDidAppear()

if let section = form.sectionBy(tag: "contacts-selected") {
    section.removeAll()
    guard let contacts = dataprovider.getContacts() else{
        return
    }
    \\ does not work
    section.header?.title = "Contacts: \(contacts.count) selected" 

    for item in contacts {
        let row = Label() {
            $0.title = item.description 
        }
        section.append(row)
    }
}

the problem is that I need to change the section title.


Solution

  • I had been working on your question an here are my results, first of all you need to be sure that your section have the tag that you need later, so you need to use this code instead of your viewDidLoad()code

    form +++ Section("contacts-selected"){section in
                section.tag = "contacts-selected"
        }
    

    and later you can get your section and change the title, but if you don´t call section.reload() in the interface will never be updated, so add section.reload() below of your section.header?.title = "Contacts: \(contacts.count) selected"

        if let section = form.sectionBy(tag: "contacts-selected") {
        section.removeAll()
        guard let contacts = dataprovider.getContacts() else{
            return
        }
        \\ does not work
        section.header?.title = "Contacts: \(contacts.count) selected" 
        section.reload() //this is the important part to update UI
    
        for item in contacts {
            let row = Label() {
                $0.title = item.description 
            }
            section.append(row)
        }
       }
    

    Here is a little example code

    //
    //  ViewController.swift
    //  EurekaExamplesSwift3
    //
    //  Created by Reinier Melian on 11/5/16.
    //  Copyright © 2016 Reinier Melian. All rights reserved.
    //
    
    import UIKit
    import Eureka
    
    class ViewController: FormViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            form = Section("Section1")
                <<< TextRow(){ row in
                    row.title = "Text Row"
                    row.placeholder = "Enter text here"
                }.onCellSelection({ (textCell, textRow) in
                    if let section = self.form.sectionBy(tag: "contacts-selected") {
    
                        section.header?.title = "Header Changed"
                        section.reload()
    
                    }
                })
                <<< PhoneRow(){
                    $0.title = "Phone Row"
                    $0.placeholder = "And numbers here"
                }
                +++ Section("Section2")
                <<< DateRow(){
                    $0.title = "Date Row"
                    $0.value = NSDate(timeIntervalSinceReferenceDate: 0) as Date
            }
    
            form +++ Section("Contacts"){section in
                    section.tag = "contacts-selected"
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

    I hope this helps you, best regards