繁体   English   中英

无法在 swift 3 中显示表格视图?

[英]Unable to display table view in swift 3?

在这里,我放置了一个集合视图,并在其下方放置了一个表视图,我使用按钮放置了切换到集合视图或表视图的逻辑,但数据未显示在表视图上,并且在集合视图中显示正常任何人都可以帮助我如何解决它?

@IBAction func listViewAction(_ sender: Any) {
        if (a == 0){
            UIView.animate(withDuration: 0.3, animations: {
                self.listButton.setImage(UIImage(named: "Thumbnails"), for: .normal)
                self.tableView.delegate = self
                self.tableView.dataSource = self
                self.collectionView.alpha = 0.0
                self.tableView.alpha = 100.0
                self.a = 1
                
            })
        }
        else{
            UIView.animate(withDuration: 0.3, animations: {
                self.listButton.setImage(UIImage(named: "link"), for: .normal)
                self.collectionView.alpha = 1.0
                self.tableView.alpha = 0.0
                self.a = 0
            })
        }
    }


func listCategoryDownloadJsonWithURL() {
        let url = URL(string: listPageUrl)!
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
                    let objArr = jsonObj["items"] as? [[String:Any]]
                    self.list = objArr!.map{List(dict: $0)}
                    DispatchQueue.main.async {
                        let itemsCount = self.list.count
                        for i in 0..<itemsCount {
                            let customAttribute = self.list[i].customAttribute
                            for j in 0..<customAttribute.count {
                                if customAttribute[j].attributeCode == "image" {
                                    let baseUrl = "http://192.168.1.11/magento2/pub/media/catalog/product"
                                    self.listCategoryImageArray.append(baseUrl + customAttribute[j].value)
                                }
                            }
                        }
                        self.activityIndicator.stopAnimating()
                        self.activityIndicator.hidesWhenStopped = true
                        self.collectionView.reloadData()
                        self.collectionView.isHidden = false
                        self.tableView.reloadData()
                    }
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }

这是此布局,此处列表按钮位于视图的左上角,如图所示

在此处输入图片说明

尝试重新加载 listViewAction 上的数据并使用 isHidden 来显示和隐藏

@IBAction func listViewAction(_ sender: Any) {
            if (a == 0){
                UIView.animate(withDuration: 0.3, animations: {
                    self.listButton.setImage(UIImage(named: "Thumbnails"), for: .normal)
                    self.tableView.delegate = self
                    self.tableView.dataSource = self
                    self.collectionView.alpha = 0.0
                    self.tableView.alpha = 1
                    self.tableView.isHidden = false
                    self.collectionView.isHidden = true
                    self.a = 1
                    self.tableView.reloadData()

                })
            }
            else{
                UIView.animate(withDuration: 0.3, animations: {
                    self.listButton.setImage(UIImage(named: "link"), for: .normal)
                    self.collectionView.alpha = 1.0
                    self.tableView.alpha = 0.0
                    self.tableView.isHidden = false
                    self.collectionView.isHidden = true
                    self.collectionView.reloadData()
                    self.a = 0
                })
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM