Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement