TableView: sections

yolo0123

Membre confirmé
16 Septembre 2018
12
0
64
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.

Bloc de code:
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
    }
}
 
Dernière édition:
Bonjour,
Le point d'arrêt c'est le fait de cliquer sur le numéro de la ligne et qu'une flèche bleu apparait ? C'est ce que j'ai fait mais il n'y a rien d'afficher dans la fenêtre dédié après avoir build et exécuter le projet. J'ai mis le code print(tabCellules.count) après la ligne tabCellules = [Cellules(sections: "Section1", items: ["obj1","obj2"]), Cellules(sections: "Section2", items: ["obj1","obj2"])] dans la fonction viewDidLoad() et ca me renvoie 2.
 
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath as IndexPath) as UITableViewCell

As-tu créé une classe pour le prototype de cellule ? A la place de "UITableViewCell", ne faut-il pas mettre le nom de la classe en question ?
J'arrive à faire fonctionner ton code avec cette modification, et en ayant ViewController de classe UITableViewController
 
J'avais parlé trop vite.
Avec le code ci-dessous, ça fonctionne pour moi

Bloc de code:
//
//  ViewController.swift
//  test
//
//  Created by Nicolas on 20/09/2018.
//  Copyright © 2018 Nicolas. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {

   

    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"])]
        print("number of sections \(tabCellules.count)")
        tableView.delegate = self
        tableView.dataSource = self
       // tableView.reloadData()
    }
   
    override 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
    }
   
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("number of rows for section \(section) = \(tabCellules[section].items.count)")
        return tabCellules[section].items.count
        //return 2
    }
   
//    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        print("number of rows for section \(section) = \(tabCellules[section].items.count)")
//        return tabCellules[section].items.count
//    }
   
    override func numberOfSections(in tableView: UITableView) -> Int {
        print("number of sections \(tabCellules.count)")
        return tabCellules.count
    }
   
//    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//        print("number of sections \(tabCellules.count)")
//        return tabCellules.count
//    }
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return tabCellules[section].sections
    }
}
 
J'obtiens ce message d'erreur lorsque j'essaye de build:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] instantiated view controller with identifier "UIViewController-pQ6-mL-gdo" from storyboard "Main", but didn't get a UITableView.'
 
Je viens de supprimer mon view contrôler pour repartir d'un MainStoryboard vierge, puis de faire glisser un UITableViewController. La table view est créée automatiquement et est liée au controller.
Ensuite:
- le déclarer en initial view controller
- mettre "myCell" en identifiant de la cellule
- déclarer le View Contrôler en classe "ViewController"

Capture d’écran 2018-09-20 à 22.32.54.png