Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // TableViewDemo
  4. //
  5. // Created by Bilguun Batbold on 22/2/19.
  6. // Copyright © 2019 ISEM. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  12.  
  13. //set array of users (in reality this is usually from a network call
  14. 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")]
  15.  
  16.  
  17. //declare an outlet and connect to the tableview previously created
  18. @IBOutlet weak var usersTableView: UITableView!
  19.  
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22.  
  23. //set the delegate and datasource to self
  24. usersTableView.delegate = self
  25. usersTableView.dataSource = self
  26. }
  27.  
  28.  
  29. //return number of items
  30. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  31. return users.count
  32. }
  33.  
  34. //return cell per row
  35. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  36. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
  37. return UITableViewCell()
  38. }
  39. cell.textLabel?.text = users[indexPath.row].name
  40. cell.detailTextLabel?.text = users[indexPath.row].gender
  41.  
  42. return cell
  43.  
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement