我使用json解析得到以下数据,但是我无法在导航栏标题,pricelabel和textview中显示它,这是我在storyboard中放置的。如何将图像组中的图像也放入其中。有人能帮助我吗?
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()
}
}
答案 0 :(得分:1)
您正在做的错误是您正在后台线程中更新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()
}
}
答案 1 :(得分:0)
在从网络接收数据之前,您似乎正在将数据设置为导航标题。
尝试在viewDidLoad中执行的操作
self.navigationbar.topItem?.title = productName
但是在完成处理程序中执行此操作。还可以尝试调试完成处理程序中的数据,以确保您实际收到的内容。
问题的其余部分有点难以理解,请重新说明。
答案 2 :(得分:0)
要更改导航栏标题,请尝试使用navigationItem.title = productName
。
对于其他人,您需要在productDescriptionlbl.text = productdescription
之后productPricelbl.text = productprice
和downloadJsonWithURL()
之后致电viewDidLoad