我遇到了这个问题的障碍。我已经尝试了这些URL1URL2进行指导,但是没有成功。如何修复未填充UITableView的JSON?我在让我的数组在TableView中禁用时遇到问题。

这是我的代码:

    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 {
            // 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
        }
       // @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("masked", forHTTPHeaderField: "X-API-KEY")
            request.setValue("masked=", 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.FilmsView.reloadData()
                               // })

                            }
                        }

                    }
                  print("Hello")
                   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.
    }

["status": 1, "films": <__NSSingleObjectArrayI 0x7ae40320>(
{
    Stores =     (
                {
            Assets =             (
                                {
                    "asset_name" = Screener;
                    "asset_status" = Needed;
                },
                                {
                    "asset_name" = Feature;
                    "asset_status" = Needed;
                },
                                {
                    "asset_name" = Trailer;
                    "asset_status" = Needed;
                },
                                {
                    "asset_name" = Artwork;
                    "asset_status" = Needed;
                },
                                {
                    "asset_name" = "Closed Caption";
                    "asset_status" = Needed;
                },
                                {
                    "asset_name" = Meta;
                    "asset_status" = Needed;
                }
            );
            Status = Declined;
            "store_name" = "Netflix Watch <br> Instantly (SD)";
        }
    );
    title = "The Red Tail";
}
)
]

最佳答案

将最后一个标题放入FilmArray后,请调用tableview.reloadData()。您必须在主队列上执行此操作,否则会得到意外的结果。

10-06 12:47