简体   繁体   中英

UITableView Error, Swift, JSON

I am taking a business applications course in Swift and am having trouble running my code. I think I have all the correct code in place to run this. I have a JSON file that I have to import into the app and push the data from the json file into the tableView. Something is breaking in the "func tableView", but I cant figure it out. Any help is appreciated :)

I added a screenshot of my code, as well as the main.storyboard

import UIKit

struct Movie : Decodable {
    let MOVIENAME : String
    let DIRECTORNAME : String
}

class ViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate {
    //indicates this is an array of the structure 'Movie'
    var movies = [Movie]()

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
        UITableViewCell {
            let myCell = movieTableView.dequeueReusableCell(withIdentifier:
                "movieTable")
            myCell?.textLabel?.text = movies[indexPath.row].MOVIENAME
            myCell?.detailTextLabel?.text = movies[indexPath.row].DIRECTORNAME
            return myCell!
    }

    @IBOutlet weak var movieTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // construct path to the file, this tells the system the name of the file and the file type JSON
        let path = Bundle.main.path(forResource: "movies", ofType: "json")
        let url = URL(fileURLWithPath: path!) //creating url of the file, add "!" to unwrap the path

        do {
            //'try" is a security measure that basically says, if we dont get successfully create data through the url
            let data = try Data(contentsOf: url)
            self.movies = try JSONDecoder().decode([Movie].self, from: data)

            for eachMovie in self.movies {
                print(eachMovie.MOVIENAME)
            }
        } catch {
            print("JSON Error.")
        }

        movieTableView.delegate = self
        movieTableView.dataSource = self
    }
}

Point 1

From code and screenshot it looks like that you have set different cell identifier than used in code(Guess!!). So cell returns nil and app will crash.

let myCell = movieTableView.dequeueReusableCell(withIdentifier: 
"movieTable")

So make sure that cell identifier in storyboard is movieTable , as in code.

Point 2

Second point is that you are using default label of cell in code textLabel and detailTextLabel .While in storyboard it shows that you have set your custom labels. Which are nowhere in use. So according to your code your labels in storyboard is not in use. So delete that table and set cell style as SubTitle to use below code.

myCell?.textLabel?.text = movies[indexPath.row].MOVIENAME
myCell?.detailTextLabel?.text = movies[indexPath.row].DIRECTORNAME

在此处输入图片说明

Point 3

If you want to make use of label in cell in custom way, you can create custom class of tableview cell. To make custom UITableViewCell .

Have you registered UITableView with UITableViewCell Identifier? On viewDidLoad() , put this and try it

YOUR_TABLE_VIEW.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "movieTable")

as @technerd said, make sure that cell identifier in storyboard is movieTable, as in code.

Hope this helps.

1 > You need to check your cell Identifier. is it set properly in storyboard? 2 > I think you have to reload your table view after filling your "[movies]".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM