Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. class TableViewController: UITableViewController {
  2. let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
  3.  
  4. // cell reuse id (cells that scroll out of view can be reused)
  5. let cellReuseIdentifier = "cell"
  6.  
  7. // don't forget to hook this up from the storyboard
  8. @IBOutlet var personList: UITableView!
  9.  
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. // Register the table view cell class and its reuse id
  13. self.personList.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
  14.  
  15. // (optional) include this line if you want to remove the extra empty cell divider lines
  16. // self.tableView.tableFooterView = UIView()
  17.  
  18. // This view controller itself will provide the delegate methods and row data for the table view.
  19. tableView.delegate = self
  20. tableView.dataSource = self
  21. }
  22.  
  23. // number of rows in table view
  24. func personList(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  25. return self.animals.count
  26. }
  27.  
  28. // create a cell for each table view row
  29. func personList(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  30.  
  31. // create a new cell if needed or reuse an old one
  32. let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
  33.  
  34. // set the text from the data model
  35. cell.textLabel?.text = self.animals[indexPath.row]
  36.  
  37. return cell
  38. }
  39.  
  40. // method to run when table view cell is tapped
  41. func personList(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  42. print("You tapped cell number \(indexPath.row).")
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement