我使用 json 解析获得以下数据,但我无法在我放置在故事板中的导航栏标题、价格标签和文本视图上显示它。如何将图像组中的图像也放入其中。有人可以帮助我吗?
class ViewController: UIViewController {
@IBOutlet weak var navigationbar: UINavigationBar!
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productPricelbl: UILabel!
@IBOutlet weak var productDescriptionlbl: UITextView!
var productName :String?
var productprice :String?
var productdescription :String?
var thumbnailimageArray = [String]()
var imageArray = [String]()
let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
// let imgURL = NSURL(string:imageArray[0])
// if imgURL != nil {
// let data = NSData(contentsOf: (imgURL as URL?)!)
// self.productImage.image = UIImage(data: data! as Data)
// }
self.navigationbar.topItem?.title = productName
productDescriptionlbl.text = productdescription
productPricelbl.text = productprice
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func arrowmarkButton(_ sender: Any) {
}
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "Detail")!)
if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
if let detailDict = detailsArray[0] as? NSDictionary {
if let name = detailDict.value(forKey: "productName") {
self.productName = name as? String
}
if let thumbnailimage1 = detailDict.value(forKey: "thumnail1"){
self.thumbnailimageArray.append(thumbnailimage1 as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail2"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail3"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail4"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail5"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail6"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail7"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail8"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail9"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail10"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let image1 = detailDict.value(forKey: "image1"){
self.imageArray.append(image1 as! String)
}
if let image2 = detailDict.value(forKey: "image2"){
self.imageArray.append(image2 as! String)
}
if let image3 = detailDict.value(forKey: "image3"){
self.imageArray.append(image3 as! String)
}
if let image4 = detailDict.value(forKey: "image4"){
self.imageArray.append(image4 as! String)
}
if let image5 = detailDict.value(forKey: "image5"){
self.imageArray.append(image5 as! String)
}
if let image6 = detailDict.value(forKey: "image6"){
self.imageArray.append(image6 as! String)
}
if let image7 = detailDict.value(forKey: "image7"){
self.imageArray.append(image7 as! String)
}
if let image8 = detailDict.value(forKey: "image8"){
self.imageArray.append(image8 as! String)
}
if let image9 = detailDict.value(forKey: "image9"){
self.imageArray.append(image9 as! String)
}
if let image10 = detailDict.value(forKey: "image10"){
self.imageArray.append(image10 as! String)
}
if let price = detailDict.value(forKey: "productPrice") {
self.productprice = price as? String
}
if let description = detailDict.value(forKey: "productDes") {
self.productdescription = description as? String
print(self.productdescription)
}
}
}
OperationQueue.main.addOperation({
})
}
}).resume()
}
}
您正在执行的错误是您在后台线程中更新 UI 元素。如果要更新 UI 元素,请先进入主线程,然后更新 UI 元素。
立即尝试这种方式
class ViewController: UIViewController {
@IBOutlet weak var navigationbar: UINavigationBar!
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productPricelbl: UILabel!
@IBOutlet weak var productDescriptionlbl: UITextView!
var productName :String?
var productprice :String?
var productdescription :String?
var thumbnailimageArray = [String]()
var imageArray = [String]()
let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
// let imgURL = NSURL(string:imageArray[0])
// if imgURL != nil {
// let data = NSData(contentsOf: (imgURL as URL?)!)
// self.productImage.image = UIImage(data: data! as Data)
// }
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func arrowmarkButton(_ sender: Any) {
}
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "Detail")!)
if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
if let detailDict = detailsArray[0] as? NSDictionary {
if let name = detailDict.value(forKey: "productName") {
self.productName = name as? String
}
if let thumbnailimage1 = detailDict.value(forKey: "thumnail1"){
self.thumbnailimageArray.append(thumbnailimage1 as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail2"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail3"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail4"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail5"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail6"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail7"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail8"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail9"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let thumbnailimage = detailDict.value(forKey: "thumnail10"){
self.thumbnailimageArray.append(thumbnailimage as! String)
}
if let image1 = detailDict.value(forKey: "image1"){
self.imageArray.append(image1 as! String)
}
if let image2 = detailDict.value(forKey: "image2"){
self.imageArray.append(image2 as! String)
}
if let image3 = detailDict.value(forKey: "image3"){
self.imageArray.append(image3 as! String)
}
if let image4 = detailDict.value(forKey: "image4"){
self.imageArray.append(image4 as! String)
}
if let image5 = detailDict.value(forKey: "image5"){
self.imageArray.append(image5 as! String)
}
if let image6 = detailDict.value(forKey: "image6"){
self.imageArray.append(image6 as! String)
}
if let image7 = detailDict.value(forKey: "image7"){
self.imageArray.append(image7 as! String)
}
if let image8 = detailDict.value(forKey: "image8"){
self.imageArray.append(image8 as! String)
}
if let image9 = detailDict.value(forKey: "image9"){
self.imageArray.append(image9 as! String)
}
if let image10 = detailDict.value(forKey: "image10"){
self.imageArray.append(image10 as! String)
}
if let price = detailDict.value(forKey: "productPrice") {
self.productprice = price as? String
}
if let description = detailDict.value(forKey: "productDes") {
self.productdescription = description as? String
print(self.productdescription)
}
}
}
OperationQueue.main.addOperation({
self.navigationbar.topItem?.title = self.productName
self.productDescriptionlbl.text = self.productdescription
self.productPricelbl.text = self.productprice
})
}
}).resume()
}
}
似乎您在从网络接收数据之前将数据设置为导航标题。
尝试执行您在viewDidLoad中执行的操作
self.navigationbar.topItem?.title = productName
而是在完成处理程序中执行此操作。此外,请尝试调试完成处理程序中的数据,以确保您确实收到了一些东西。
其余的问题有点难以理解,请重新措辞。
要更改导航栏标题,请尝试使用 navigationItem.title = productName
。其余的,您需要在downloadJsonWithURL()
后致电productDescriptionlbl.text = productdescription
和productPricelbl.text = productprice
,而不仅仅是在viewDidLoad