Advertisement
Guest User

tableview

a guest
Nov 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // TableViewTest
  4. //
  5. // Created by Tobias Ahrenschneider Sztuk on 08/11/2019.
  6. // Copyright © 2019 Tobias Ahrenschneider Sztuk. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, UITableViewDataSource {
  12.  
  13.  
  14. @IBOutlet weak var textLabel: UILabel!
  15. var score = 0 {
  16. didSet{
  17. textLabel.text = "Score: \(score)"
  18. }
  19. }
  20. var scoreArray: [String] = []
  21.  
  22. @IBOutlet weak var scoreTableView: UITableView!
  23.  
  24.  
  25. @IBOutlet weak var timerLabel: UILabel!
  26. var timer = Timer()
  27. var counter = 3
  28.  
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. // Do any additional setup after loading the view.
  32.  
  33. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
  34. scoreTableView.dataSource = self
  35. }
  36.  
  37. @objc func timerAction() {
  38. if counter > 0{
  39. counter -= 1
  40. timerLabel.text = "\(counter)"
  41. }else{
  42. scoreArray.append("\(score)")
  43. scoreTableView.reloadData()
  44. counter = 10
  45. }
  46.  
  47. }
  48.  
  49. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  50. if scoreArray.count < 5 {
  51. return scoreArray.count
  52. }else{
  53. return 5
  54. }
  55.  
  56. }
  57.  
  58. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  59. let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexPath)
  60.  
  61. // Configure the cell...
  62. cell.textLabel?.text = scoreArray[indexPath.row]
  63.  
  64. return cell
  65. }
  66.  
  67. @IBAction func buttonPress(_ sender: UIButton) {
  68. score += 1
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement