Skip to content

Instantly share code, notes, and snippets.

@kanic61
Forked from licvido/app.swift
Created March 1, 2018 03:51
Show Gist options
  • Save kanic61/29c4a5dad7d726da22a7e42f12c10ead to your computer and use it in GitHub Desktop.
Save kanic61/29c4a5dad7d726da22a7e42f12c10ead to your computer and use it in GitHub Desktop.
SWIFT: UITableView sections example
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var itemsInSections: Array<Array<String>> = [["1A", "1B", "1C"], ["2A", "2B"], ["3A", "3B", "3C", "3D", "3E"]]
var sections: Array<String> = ["Section 1", "Section 2", "Section 3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemsInSections[section].count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
var text = self.itemsInSections[indexPath.section][indexPath.row]
cell.textLabel!.text = text
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment