Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Last active February 22, 2019 02:41
Show Gist options
  • Save Bilguun132/52fd5371b9b20bb47519c3f702de8a32 to your computer and use it in GitHub Desktop.
Save Bilguun132/52fd5371b9b20bb47519c3f702de8a32 to your computer and use it in GitHub Desktop.
TableView-Tutorial-ViewController(no refactor)
//
// ViewController.swift
// TableViewDemo
//
// Created by Bilguun Batbold on 22/2/19.
// Copyright © 2019 ISEM. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//set array of users (in reality this is usually from a network call
private let users: [User] = [User(name: "Albert", gender: "Male", email: "albert@gmail.com"), User(name: "Bob", gender: "Male", email: "bob@gmail.com"), User(name: "Celine", gender: "Female", email: "celine@gmail.com"), User(name: "Derrick", gender: "Male", email: "derrick@gmail.com"), User(name: "Aldwin", gender: "Male", email: "aldwin@gmail.com")]
//declare an outlet and connect to the tableview previously created
@IBOutlet weak var usersTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//set the delegate and datasource to self
usersTableView.delegate = self
usersTableView.dataSource = self
}
//return number of items
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
//return cell per row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
cell.textLabel?.text = users[indexPath.row].name
cell.detailTextLabel?.text = users[indexPath.row].gender
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment