如何修复 JSON 不填充我的 UITableView?
Posted
技术标签:
【中文标题】如何修复 JSON 不填充我的 UITableView?【英文标题】:How do I fix JSON not populating my UITableView? 【发布时间】:2017-02-02 21:50:04 【问题描述】:我遇到了这个问题。我已经尝试过这些URL1
和URL2
以获得指导,但没有成功。如何修复未填充我的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";
)
]
【问题讨论】:
每次数据源更改时,您都需要在 tableview 上调用 reloadDate。在您的代码中,完成附加所有电影标题后,调用:self.tableView.reloadData() 不用说,调用必须在mainQueue上完成 【参考方案1】:将最后一个标题放入FilmArray
后,调用tableview.reloadData()
。你必须在主队列上做,否则你会得到意想不到的结果。
【讨论】:
以上是关于如何修复 JSON 不填充我的 UITableView?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用来自 json 的 mutablearray 填充我的 tableview