Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // TableViewTest
- //
- // Created by Tobias Ahrenschneider Sztuk on 08/11/2019.
- // Copyright © 2019 Tobias Ahrenschneider Sztuk. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController, UITableViewDataSource {
- @IBOutlet weak var textLabel: UILabel!
- var score = 0 {
- didSet{
- textLabel.text = "Score: \(score)"
- }
- }
- var scoreArray: [String] = []
- @IBOutlet weak var scoreTableView: UITableView!
- @IBOutlet weak var timerLabel: UILabel!
- var timer = Timer()
- var counter = 3
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
- scoreTableView.dataSource = self
- }
- @objc func timerAction() {
- if counter > 0{
- counter -= 1
- timerLabel.text = "\(counter)"
- }else{
- scoreArray.append("\(score)")
- scoreTableView.reloadData()
- counter = 10
- }
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if scoreArray.count < 5 {
- return scoreArray.count
- }else{
- return 5
- }
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexPath)
- // Configure the cell...
- cell.textLabel?.text = scoreArray[indexPath.row]
- return cell
- }
- @IBAction func buttonPress(_ sender: UIButton) {
- score += 1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement