UITable Won't display. Value type 'FilmsAPITableViewCell' has no member 'movieTitle'

跟風遠走 提交于 2019-12-12 01:43:47

问题


I am getting build errors when trying to display jSON data in Xcode using Swift 3. I am going to copy a portion of my code to this page with hopes you guys can assist me.

I have found similar questions on this site however answers don't seem to help.

 class FilmsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    weak var tableView : UITableView!
    var FilmArray = [String]()

    let film_url = "https://www.testing.com/api/resources/films/1"
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as! FilmsAPITableViewCell        
        // Adding the right informations
        cell.movieTitle.text = FilmArray[indexPath.row]
        // Returning the cell
        return cell
    }
    // @IBOutlet weak var FilmsView: UITableView!
    //    weak var tableView : UITableView!
    //    var FilmArray = [String]()
    //
    //    let film_url = "https://www.distribber.com/api/resources/films/1"
    //
    override func viewDidLoad() {
        super.viewDidLoad()

        let tableView = UITableView (frame:view.bounds)
        view.addSubview(tableView)
        self.tableView = tableView

        tableView.dataSource = self
        tableView.delegate = self


        //        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //            return 1
        //        }
        //            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //                // Getting the right element
        //               //let films = FilmArray[indexPath.row]
        //
        //
        //                // Instantiate a cell
        //                //let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "moviecell")
        //                let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FilmsAPITableViewCell
        //                //        cell.movieTitle.text = FilmArray[indexPath.row]
        //                // Adding the right informations
        //                cell.movieTitle.text = FilmArray[indexPath.row]
        //                // Returning the cell
        //                return cell
        //        }
        // }

        //}



        let url:URL = URL(string: film_url)!
        let session = URLSession.shared

        let request = NSMutableURLRequest(url: url)
        request.httpMethod = "GET"
        request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
        request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        let paramString = ""


        //        for (key, value) in post_data
        //        {
        //            paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
        //        }
        //
        request.httpBody = paramString.data(using: String.Encoding.utf8)

        let task = session.dataTask(with: request as URLRequest, completionHandler: {
            (
            data, response, error) in

            guard let _:Data = data, let _:URLResponse = response  , error == nil else {

                return
            }



            let json: Any?

            do
            {
                json = try JSONSerialization.jsonObject(with: data!, options: [])

                //  Prasing JSON
                var parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
                print(parsedData)
                if let FilmArray = parsedData["films"] as? NSArray {
                    for movieTitle in FilmArray{
                        if let filmDict = movieTitle as? NSDictionary{
                            if let film = filmDict.value(forKey: "title") {
                                self.FilmArray.append(film as! String)
                            }

                            OperationQueue.main.addOperation({
                            self.tableView.reloadData()
                             })

                        }
                    }

                }
                print("Hello")
                self.tableView.reloadData()
                print(self.FilmArray)
            }
            catch
            {
                return
            }

            guard let server_response = json as? NSDictionary else
            {
                return
            }


            if let data_block = server_response["data"] as? NSDictionary
            {
                if let session_data = data_block["session"] as? String
                {
                    //  self.login_session = session_data

                    let preferences = UserDefaults.standard
                    preferences.set(session_data, forKey: "session")

                    //  DispatchQueue.main.async(execute: self.LoginDone)
                }
            }



        })

        task.resume()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

Here is also output from FilmsAPITableViewCell.swift

import UIKit

import UIKit

class FilmsAPITableViewCell: UITableViewCell {
    @IBOutlet weak var movieTitle: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

回答1:


The reason why movieTitle is nil is because your custom cell class does not have that label outlet. You need to create a UILabel in your .xib (or storyboard) and create an outlet connection inside your custom cell class.

It also seems like no self.tableView.reloadData() exists in your completion block. Try adding that right after the print("Hello") line.

P.S. Don't forget to dispatch the reload to the main queue.

Here is also your code which I edited as to get it working:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
    tableView.delegate = self

    let url:URL = URL(string: film_url)!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY")
    request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization")
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
    let paramString = ""

    request.httpBody = paramString.data(using: String.Encoding.utf8)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (
        data, response, error) in

        guard let _:Data = data, let _:URLResponse = response  , error == nil else {

            return
        }



        var json:Any?

        do
        {
            if let existingData = data {
                json = try JSONSerialization.jsonObject(with: existingData, options: [])
            }

            //  Prasing JSON
            if let parsedData = json as? [[String:Any]] {
                for dict in parsedData {
                    if let title = dict["title"] as? String {
                        self.FilmArray.append(title)
                    }
                }

                OperationQueue.main.addOperation({
                    self.tableView.reloadData()
                })
            }
        }
        catch
        {
            return
        }

        guard let server_response = json as? NSDictionary else
        {
            return
        }

        if let data_block = server_response["data"] as? NSDictionary
        {
            if let session_data = data_block["session"] as? String
            {
                //  self.login_session = session_data

                let preferences = UserDefaults.standard
                preferences.set(session_data, forKey: "session")

                //  DispatchQueue.main.async(execute: self.LoginDone)
            }
        }
    })

    task.resume()
}


来源:https://stackoverflow.com/questions/42011288/uitable-wont-display-value-type-filmsapitableviewcell-has-no-member-movieti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!