1

我正在尝试实现Swift Programming in Easy Steps 练习中第 5 章中的 TableView 示例。我已经检查并重新检查了示例代码(甚至下载并测试了实际的示例代码),但我仍然收到此运行时错误。有谁知道为什么会这样?

2019-11-01 07:56:51.247052+0100 TableView_EasySteps[2067:39485] 无法结束 BackgroundTask:不存在标识符为 1 (0x1) 的后台任务,或者它可能已经结束。中断 UIApplicationEndBackgroundTaskError() 进行调试。

这是 ViewController 代码:

import UIKit

class WebsitesTableViewController: UITableViewController {

    var websites:[[String]] = [
        ["Apple",           "https://www.apple.com"]   ,
        ["NY Times",        "https://www.nytimes.com"] ,
        ["DN",              "https://www.dn.se"]   ,
        ["NFL",             "https://www.nfl.com"]   ,
        ["Premier League",  "https://www.premierleague.com"]   ,
        ["The Guardian",    "https://www.theguardian.com"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // preserve selection between presentations
        self.clearsSelectionOnViewWillAppear = false
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")
        }
        cell!.textLabel!.text = websites[indexPath.row][0]
        cell!.detailTextLabel!.text = websites[indexPath.row][1]

        return cell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let url = URL(string: websites[indexPath.row][1]) {
            UIApplication.shared.open(url)
        }
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            websites.remove(at: indexPath.row)
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
}
4

1 回答 1

0

如果您使用 MVC 标准,请检查 SceneDelegate.swift 文件并确保它是控制文件的一部分,否则只需确保它与 AppDelegate.swift 和 ViewController.swift 等一起是 Xcode 项目中文件的一部分。

于 2019-11-20T11:09:38.943 回答