我曾尝试在 ui 上显示收藏视图,但无法显示任何人都可以帮助我吗? [复制]

Posted

技术标签:

【中文标题】我曾尝试在 ui 上显示收藏视图,但无法显示任何人都可以帮助我吗? [复制]【英文标题】:I had tried displaying collection view on the ui but unable to display can anyone help me? [duplicate] 【发布时间】:2017-05-20 11:16:15 【问题描述】:

在这里我尝试实现集合视图但无法执行此操作,并且在滚动视图上我放置了集合视图如果不能这样实现是否正确,您可以提供任何解决方案以显示在 ui 和 在这个滚动视图中已经实现了视图并且所有元素都放在它上面是我的代码以及如何缩放选定的特定图像也有人可以帮助我吗?

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

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

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

    override func viewDidLoad() 
        super.viewDidLoad()
        self.downloadJsonWithURL()
        collectionView.delegate = self
        collectionView.dataSource = self
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped(gesture:)))
        swipeLeft.direction = .left
        self.imageView.addGestureRecognizer(swipeLeft)
        swipeLeft.cancelsTouchesInView = false
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped(gesture:)))
        swipeRight.direction = .right
        self.imageView.addGestureRecognizer(swipeRight)
        swipeRight.cancelsTouchesInView = false
        imageView.isUserInteractionEnabled = true
        // 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.append(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.append(price as! String)
                        
                        if let description = detailDict.value(forKey: "productDes") 
                            self.productdescription = description as? String
                        
                        if let image = detailDict.value(forKey: "img")
                            self.imagesArray.append(image as! String)
                        
                    
                
                OperationQueue.main.addOperation(
                    self.navigationBar.topItem?.title = self.productName[0]
                    self.textview.text = self.productdescription
                    self.priceLabel.text = self.productprice[0]
                    self.nameLabel.text = self.productName[0]
                    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
                
                let imgURL = NSURL(string:self.imageArray[pageIndex])
                let data = NSData(contentsOf: (imgURL as URL?)!)
                self.imageView.image = UIImage(data: data! as Data)
                pageControl.numberOfPages = imageArray.count
                pageControl.currentPage = pageIndex
            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)
                pageControl.numberOfPages = imageArray.count
                pageControl.currentPage = pageIndex
            default:
                break
            
        
    
    func numberOfSections(in collectionView: UICollectionView) -> Int 
        return 1
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
        return imagesArray.count
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for:indexPath) as! DetailsCell
        let arr = imagesArray[indexPath.row]
        let urls = NSURL(string: arr)
        let data = NSData (contentsOf: urls! as URL)
        cell.imageView.image = UIImage(data: data! as Data)
        cell.nameLabel.text = self.productName[indexPath.row]
        cell.priceLabel.text = self.productprice[indexPath.row]
        cell.layer.borderColor = UIColor.lightGray.cgColor
        cell.layer.borderWidth = 0.7
     return cell
    

【问题讨论】:

您不久前使用相同的代码发布了一个问题,甚至接受了一个答案。 ***.com/questions/44082366/…你在寻找答案吗?? @Vamsi 你现在的展示是什么?您可以提供图片。 @bilal 我尝试在同一任务中实现图像视图但无法在其上显示这就是我发布的原因 我手机 7 显示尺寸我尝试显示图像但无法发布 i.stack.imgur.com/QWYQr.png 这是图片链接@aircraft 【参考方案1】:

web service response 获取所有data 后重新加载collection view,如下所示。

collectionView.reloadData()

【讨论】:

它只在 ui 上的集合视图时工作,但我有这样的页面,它不起作用 i.stack.imgur.com/QWYQr.png 我没听懂你说的。 it was working when only collection view on the ui。那么你的收藏视图在哪里 i.stack.imgur.com/QWYQr.png 您只需在此处查看图像我在描述下方呈现集合视图【参考方案2】:

downloadJsonWithURL 加载后需要重新加载collectionview的数据。

【讨论】:

如何重新加载集合视图,代码是什么? 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 // your code DispatchQueue.main.async // Update your UI here // Reload collectionView data here collectionView.reloadData() ).resume()

以上是关于我曾尝试在 ui 上显示收藏视图,但无法显示任何人都可以帮助我吗? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

D3 svg图表无法在IE中显示

尝试在列表视图中显示每个文件上传的进度时 UI 卡住

iOS 14 / UI 测试 DatePicker

如何在收藏视图中显示复选标记(图像)

谷歌地图视图无法在模拟器上显示

我正在从 json Web 服务获取数据,但无法在我的 ui 上显示它