0
votes

Comment utiliser la vue personnalisée avec Swift?

Je veux ajouter une vue personnalisée dans la TableViewheader. Mais lorsque j'exécute le code suivant, il crée un cycle et une application bloqués pour toute interaction utilisateur.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 60)
    let expandabelView = ExpandableView(frame: frame)
    return expandabelView
}


2 commentaires

Toujours pas en train de travailler le même problème


init (codeur :) appelle setupview () qui appelle uinib (nibname: bundle :) il rappellera donc init ( Codeur :) . Alors pourquoi appelez-vous uinib (nibname: Bundle :) dans initwithcoder ?


3 Réponses :


0
votes

Lorsque vous créez personnalisé uIView code>, il devrait suivre ceci si vous souhaitez l'utiliser init avec le cadre.

class CustomView: UIView {
    //This should be contentview of your xib
    @IBOutlet var view: UIView!

    let nibName = "CustomView"

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    private func xibSetup()
    {
        Bundle.main.loadNibNamed(nibName, owner: self, options: nil)
        self.view.autoresizesSubviews = true
        self.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(self.view)
        self.view.frame = self.frame


    }

}


0 commentaires

0
votes

Puisque vous devez Charger Vue de NIB , chargez-le, puis ajoutez Sous-View à votre vue. Ensuite, définissez le cadre image et définissez le masque automobile de la vue de contenu correctement xxx


0 commentaires

0
votes

Il peut y avoir de nombreuses autres manières, mais je vous recommande de faire votre ExpandableView code> réutilisable pour améliorer les performances.


Tout d'abord strong>, simplifie votre ExpandableView Code> Classe: P>

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView.delegate = self
        tableView.dataSource = self
        //...
        let nib = UINib(nibName: "ExpandableView", bundle: nil)
        tableView.register(nib, forHeaderFooterViewReuseIdentifier: "ExpandableView")
        tableView.estimatedSectionHeaderHeight = 60
        tableView.sectionHeaderHeight = UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let expandabelView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableView")
        // Frame size should be represented with constraints.
        return expandabelView
    }


1 commentaires

Excellente explication. Merci.