UITableViewCell 不显示数据

TableViewCell not displaying data

我有一个 table 视图单元格,它应该只显示一个标签,但当我 运行 我的应用程序时它根本不显示任何内容。我什至尝试更改我的代码中的 textLabels 但仍然没有。我也尝试更改单元格背景颜色,这在故事板中有效,但当我实际上 运行 应用程序没有任何变化。我已经完全断开我的视图控制器以查看我的代码是否是问题所在,但仍然没有任何反应,所以从我看到的不是我的代码而是视图本身。我可能是错的。这是我现在的观点:tableviewdata

//
//  TableViewController.swift
//  streamingVideo
//
//  Created by Connor Woodford on 5/15/18.
//  Copyright © 2018 Connor Woodford. All rights reserved.
//

import UIKit

struct Courses: Decodable {
    let id: String
    let title: String
    let location: String

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {



    var courses = [Courses]()

    @IBOutlet weak var arg: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        fetchJSON()


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    func fetchJSON() {

        let urlString = "http://homevideostuff.192.168.1.14.xip.io:8888/getClasses.php"
        guard let url = URL(string: urlString) else { return }
        URLSession.shared.dataTask(with: url) { (data, _, err) in
            DispatchQueue.main.async {
                if let err = err {
                    print("Failed to get data from URL:", err)
                    return
                } else {

                    print("Loaded URL")
                }

                guard let data = data else { return }

                do {
                    let decoder = JSONDecoder()
                    self.courses = try decoder.decode([Courses].self, from: data)
                    print(self.courses)

                } catch let jsonErr {
                    print("Failed to decode JSON", jsonErr)
                }
            }

            }.resume()
    }

    // MARK: - Table view data source

    func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return courses.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        let sweet = courses[indexPath.row]

        let bgColorView = UIView()
        bgColorView.backgroundColor = UIColor.black
        cell.selectedBackgroundView = bgColorView

        cell.textLabel?.text = "sdafasdfs"
        cell.detailTextLabel?.text = sweet.location

        print("azDFASDFASDFAS")
        return cell
}
}

您必须在获取后重新加载数据 course ,您的 table 名称是 arg

self.arg.reloadData() 完成获取时 courses

在那行之后

 self.courses = try decoder.decode([Courses].self, from: dat
--> self.arg.reloadData()
  func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

您需要 return 至少 1 个部分,因为一行是部分的子部分