TableViewCell不显示数据

时间:2018-05-17 15:17:00

标签: swift storyboard tableview cell

我有一个表格视图单元格,它应该只显示一个标签但是当我运行我的应用程序时它根本不会显示任何内容。我甚至尝试在我的代码中更改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
}
}

2 个答案:

答案 0 :(得分:2)

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

您需要至少返回1个部分,因为行是某个部分的子项

答案 1 :(得分:0)

您需要在获取course后重新加载数据,您的表名为arg

完成提取self.arg.reloadData()

后,

courses

之后

 self.courses = try decoder.decode([Courses].self, from: dat
--> self.arg.reloadData()