IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Swift Discussion :

tableView les sections ne s'affichent pas


Sujet :

Swift

  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 604
    Points : 206
    Points
    206
    Par défaut tableView les sections ne s'affichent pas
    Bonjour,

    Je souhaite mettre en place une tableView avec des sections mais je n'ai qu'une section qui est mis, je ne sais pas pourquoi les autres sections ne sont pas affichées

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
     
        @IBOutlet weak var tableView: UITableView!{
            didSet {
                tableView.dataSource = self
                tableView.delegate = self
            }
        }
     
        struct Cellules {
            var sections: String!
            var items: [String]!
        }
     
        var tabCellules = [Cellules]()
     
        override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
     
        tabCellules = [Cellules(sections: "Section1", items: ["obj1","obj2"]), Cellules(sections: "Section2", items: ["obj1","obj2"])]
     
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        	let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath as IndexPath) as UITableViewCell
        	cell.textLabel?.text = tabCellules[indexPath.section].items[indexPath.row]
        	return cell
        }
     
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return tabCellules[section].items.count
        }
     
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return tabCellules.count
        }
     
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return tabCellules[section].sections
        }
    }

  2. #2
    Membre habitué
    Inscrit en
    Mars 2007
    Messages
    258
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mars 2007
    Messages : 258
    Points : 127
    Points
    127
    Par défaut Réponse
    Bonjour pitchu,

    En effet, c'est très bizarre, je viens de tester chez moi et cela fonctionne sans problème.
    Ci-dessous, le code que j'ai utilisé (sans storyboard) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    import UIKit
     
    struct Cellules {
        var sections: String!
        var items: [String]!
    }
     
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
     
        let tableView = UITableView()
        var tabCellules = [Cellules]()
     
        override func viewDidLoad() {
     
            super.viewDidLoad()
            self.view.backgroundColor = .white
     
            tabCellules = [Cellules(sections: "Section1", items: ["obj1","obj2"]), Cellules(sections: "Section2", items: ["obj1","obj2"])]
     
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
            tableView.rowHeight = UITableView.automaticDimension
            tableView.translatesAutoresizingMaskIntoConstraints = false
            self.view.addSubview(tableView)
     
            print(tabCellules.count)
     
            redessine(UIScreen.main.bounds.size)
        }
     
        //MARK: - Affichage
        func redessine(_ size: CGSize) {
            let views = ["tableView": tableView]
            self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[tableView]|", options: [], metrics: nil, views: views))
            self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[tableView]|", options: [], metrics: nil, views: views))
        }
     
        //MARK: - Protocol TableView
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return tabCellules[section].items.count
        }
     
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath as IndexPath)
            cell.textLabel?.text = tabCellules[indexPath.section].items[indexPath.row]
            return cell
        }
     
        func numberOfSections(in tableView: UITableView) -> Int {
            return tabCellules.count
        }
     
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return tabCellules[section].sections
        }
     
    }
    Bien à toi

    Benjisan

Discussions similaires

  1. [AJAX] Div dynamique AJAX. Sous IE, les images ne s'affichent pas
    Par N3odyme dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 10/12/2006, 20h56
  2. Les images ne s'affichent pas dans mes JSP
    Par casho dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 16/09/2006, 20h01
  3. Templates :: les variables ne s'affichent pas
    Par gollum33 dans le forum Langage
    Réponses: 2
    Dernier message: 19/06/2006, 22h49
  4. [JAR] Les icônes ne s'affichent pas
    Par david06600 dans le forum Général Java
    Réponses: 15
    Dernier message: 19/02/2006, 14h01
  5. Les classes ne s'affichent pas
    Par karl3i dans le forum MFC
    Réponses: 8
    Dernier message: 26/01/2004, 14h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo