user8583107
user8583107

Reputation:

Unable to display table view in swift 3?

Here i had placed a collection view and below it i had placed a table view by using button i had placed the logic to switch to collection view or table view but the data was not displaying on table view and it was displaying fine in collection view can anyone help me how to resolve it ?

@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()
    }

Here is the layout for this and here list button is on top left on view as shown in image

enter image description here

Upvotes: 2

Views: 72

Answers (1)

Varun Naharia
Varun Naharia

Reputation: 5420

Try to reload data on listViewAction and use isHidden to show and hide

@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
                })
            }
        }

Upvotes: 1

Related Questions