这里我放置了一个集合视图,在它下面我已经使用按钮放置了一个表视图我已经将逻辑切换到集合视图或表视图但是数据没有显示在表视图上它显示正常集合视图有谁可以帮我解决它?
@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()
}
以下是此布局,此处列表按钮位于视图的左上角,如图所示
答案 0 :(得分:1)
尝试在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
})
}
}