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