Reputation:
I get the following data using json parsing but I am unable to display it on the navigation bar title,pricelabel and textview which I had placed in storyboard. How to place an image from group of images also into it. Can anybody help me ?
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()
}
}
Upvotes: 0
Views: 43
Reputation: 2455
The mistake you are doing is that you are updating UI elements in background thread. If you want to update UI element first come in main thread then update UI elements.
Try this way now
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()
}
}
Upvotes: 1
Reputation: 1996
To change the navigation bar title, try using navigationItem.title = productName
.
For the rest, you need to call productDescriptionlbl.text = productdescription
and productPricelbl.text = productprice
after your downloadJsonWithURL()
not just in viewDidLoad
Upvotes: 0
Reputation: 2449
It seems like you re setting data to the navigation title before having received it from the network.
Try doing what you did in viewDidLoad
self.navigationbar.topItem?.title = productName
but instead do that inside the completion handler. Also try debugging the data inside the completion handler to make sure you're actually receiving something.
The rest of the question is a little hard to understand, please rephrase.
Upvotes: 0