Connor Woodford
Connor Woodford

Reputation: 41

TableViewCell not displaying data

I have a table view cell that is supposed to display just a label but it wont show anything at all when I run my app. I have even tried changing the textLabels within my code but still nothing. I tried changing the cell background color as well which worked in the storyboard but when I actually run the app nothing changes. I have disconnected my view controller entirely to see if my code was the issue and still nothing happened so from what I see is its not my code but the view itself. I could be wrong though. Here is what my view loks like now: 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
}
}

Upvotes: 0

Views: 652

Answers (2)

CZ54
CZ54

Reputation: 5588

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

You need to return at least 1 section, as a row is a child of a section

Upvotes: 2

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

You have to reload data after fetch course , your table name is arg

self.arg.reloadData() when finish fetching courses

after that line

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

Upvotes: 0

Related Questions