gpt4 book ai didi

ios - 如何修复 JSON 未填充我的 UITableView 的问题?

转载 作者:行者123 更新时间:2023-11-30 12:43:06 25 4
gpt4 key购买 nike

我在这个问题上遇到了障碍。我已尝试使用这些 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()。您必须在主队列上执行此操作,否则您会得到意外的结果。

关于ios - 如何修复 JSON 未填充我的 UITableView 的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42012936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com