【问题标题】:Stop watch app in swift 11 (New to the coding world)swift 11 中的秒表应用程序(编码世界的新手)
【发布时间】:2020-08-10 09:16:58
【问题描述】:

我收到 4 条不同的错误消息。

  1. 类型“StopWatchViewController”不符合协议“UITableViewDataSource”
  2. 在“StopWatchViewController”的声明中
  3. 使用未解析的标识符“UITabelViewCell”
  4. 使用未解析的标识符“UITabelViewCellStyle”

//

import UIKit

class StopWatchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var laps: [String] = []

    var timer = Timer()
    var minutes: Int = 0
    var seconds: Int = 0
    var fractions: Int = 0

    var stopwatchString: String = ""

    var startStopWatch: Bool = true
    var addLap: Bool = false


    @IBOutlet weak var stopWatchLabel: UILabel!
    @IBOutlet weak var lapsTableView: UITableView!
    @IBOutlet weak var startStopButton: UIButton!
    @IBOutlet weak var lapResetButton: UIButton!


    @IBAction func startStop(_ sender: AnyObject) {

        if startStopWatch == true {

            timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: Selector(("updateStopWatch")), userInfo: nil, repeats: true)
            startStopWatch = false

            addLap = true

        }else {

            timer.invalidate()
            startStopWatch = true

            addLap = false
        }
    }

    @IBAction func lapReset(_ sender: AnyObject) {
    }
   if addLap == true {

    laps.insert(stopwatchString, atIndex: 0)
    lapsTableView.reloadData()

    }else {

    addLap = false

    fractions = 0
    seconds = 0
    minutes = 0

    stopwatchString = "00:00.00"
    stopwatchLabel.text = stopwatchString

    }


    override func viewDidLoad() {
        super.viewDidLoad()

        stopWatchLabel.text = "00:00.00"

        // Do any additional setup after loading the view.
    }

  override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    func updateStopwatch() {

        fractions += 1
        if fractions == 100 {

            seconds += 1
            fractions = 0

        }

        if seconds == 60 {

            minutes += 1
            seconds = 0
        }

        let fractionsString = fractions > 9 ? "\(fractions)" : "0\(fractions)"
        let secondsString = seconds > 9 ? "\(seconds)" : "\(seconds)"
        let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"

        stopwatchString = "\(minutesString):\(secondsString).\(fractionsString)"
        stopWatchLabel.text = stopwatchString
    }

    //Table View Methods

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return laps.count
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = UITabelViewCell(style: UITabelViewCellStyle.Value1, reuseIdentifier: "Cell")

        cell.backgroundColor = self.view.backgroundColor
        cell.textLabel.text = "Lap \(indexPath.row)"
        cell.detailTextLabel?.text = laps[indexPath.row]

        return cell

    }


}

【问题讨论】:

    标签: swift stopwatch


    【解决方案1】:
    • 你的方法 cellForRow 没有正确的签名,应该是func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    • UITabelViewCell 应拼写为UITableViewCell

    • UITabelViewCellStyle 应拼写为UITableViewCellStyle

    你应该检查你的拼写,大部分时间错误都与它有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2014-10-24
      • 2023-03-27
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      相关资源
      最近更新 更多