我从json Web服务获取数据但无法在我的ui上显示它 [英] I am getting data from json web services but unable to display it on my ui

查看:73
本文介绍了我从json Web服务获取数据但无法在我的ui上显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我遇到的问题是我能够从json网络服务获取数据,但是无法在我提到的程序中显示我的ui我在哪里得到错误以及它还提到了什么可以有人帮助我?

here I am having problem that I am able to get the data from json web services but unable to display on my ui in the program I mentioned where I am getting error and what it is also mentioned there can anyone help me ?

class DetailsViewController: UIPageViewController{

    @IBOutlet var navigationBar: UINavigationBar!
    @IBOutlet var imageView: UIImageView!
    @IBOutlet var pageControl: UIPageControl!
    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var priceLabel: UILabel!
    @IBOutlet var textview: UITextView!

    var productName :String?
    var productprice :String?
    var productdescription :String?
    var thumbnailimageArray = [String]()
    var imageArray = [String]()
    var pageIndex: Int = 0


let urlString = "http://www.json-generator.com/api/json/get/cjpberBhKa?indent=2"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadJsonWithURL()
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        swipeLeft.direction = .left
        self.imageView.addGestureRecognizer(swipeLeft) //unexpectely found nil while unwrapping an optional value
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        swipeRight.direction = .right
        self.imageView.addGestureRecognizer(swipeRight)
        imageView.isUserInteractionEnabled = true
        pageControl.numberOfPages = imageArray.count
        pageControl.currentPage = pageIndex
        // Do any additional setup after loading the view.
    }
    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 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
                        }
                    }
                }
                OperationQueue.main.addOperation({
                    self.navigationBar.topItem?.title = self.productName
                    self.textview.text = self.productdescription
                    self.priceLabel.text = self.productprice
                    print(self.imageArray)
                    let imgURL = NSURL(string:self.imageArray[0])
                    let data = NSData(contentsOf: (imgURL as URL?)!)
                    self.imageView.image = UIImage(data: data! as Data)
                })
            }
        }).resume()
    }
    func swiped(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right :
                if pageIndex == 0 {

                }else{
                    pageIndex -= 1
                }
                imageView.image = UIImage(named: imageArray[pageIndex])
            case UISwipeGestureRecognizerDirection.left:
                if pageIndex >= imageArray.count-1{

                }else{
                    pageIndex += 1
                }
                imageView.image = UIImage(named: imageArray[pageIndex])
            default:
                break
            }
        }
    }


推荐答案

你用什么来显示图像在 func swiped(手势:UIGestureRecognizer)方法执行如下

what you are using to display image in func swiped(gesture: UIGestureRecognizer) method execution is as below

imageView.image = UIImage(named: imageArray[pageIndex])

但您的图像是从服务器进行编码所以您只需要显示所有图像您的产品是在 OperationQueue.main.addOperation 中首先显示的,所以代替使用 UIImage显示图像(名称为:imageArray [pageIndex])您需要将其转换为数据并将其显示为imageview,并且修改后的 func swiped(gesture:UIGestureRecognizer)可能如下所示

but your image is comping from the server so you just need to display all images of your product is as you displayed first in OperationQueue.main.addOperation so in place of displaying image using UIImage(named: imageArray[pageIndex]) you need to convert it to data and display it to imageview and your modified func swiped(gesture: UIGestureRecognizer) may look like as below

func swiped(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right :
                if pageIndex == 0 {

                }else{
                    pageIndex -= 1
                }
                let imgURL = NSURL(string:self.imageArray[pageIndex])
                let data = NSData(contentsOf: (imgURL as URL?)!)
                self.imageView.image = UIImage(data: data! as Data)

            case UISwipeGestureRecognizerDirection.left:
                if pageIndex >= imageArray.count-1{

                }else{
                    pageIndex += 1
                }
                let imgURL = NSURL(string:self.imageArray[pageIndex])
                let data = NSData(contentsOf: (imgURL as URL?)!)
                self.imageView.image = UIImage(data: data! as Data)
            default:
                break
            }
        }
    }

这篇关于我从json Web服务获取数据但无法在我的ui上显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆