iOS Eureka Изменить заголовок раздела после создания

Я перезагружаю данные в форме Eureka в методе viewDidAppear:

у меня есть что-то вроде:

в представленииDidLoad()

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

в представленииDidAppear()

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)
    }
}

проблема в том, что мне нужно изменить название раздела.


person Hakim    schedule 31.10.2016    source источник


Ответы (1)


Я работал над вашим вопросом, и вот мои результаты, прежде всего вам нужно убедиться, что в вашем разделе есть тег, который вам понадобится позже, поэтому вам нужно использовать этот код вместо вашего viewDidLoad()code

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

а позже вы можете получить свой раздел и изменить заголовок, но если вы не назовете section.reload() в интерфейсе, он никогда не будет обновляться, поэтому добавьте section.reload() ниже вашего 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)
    }
   }

Вот небольшой пример кода

//
//  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.
    }


}

Я надеюсь, что это поможет вам, с уважением

person Reinier Melian    schedule 05.11.2016
comment
просто быстрое примечание: выполнение +++Section(){$0.tag = "tag"}, а затем изменение заголовка позже не сработает. section.header в этом случае будет равно нулю. вам нужно добавить начальный заголовок заголовка +++Section("title"){$0.tag= "tag"} - person Hakim; 07.11.2016