ios - UICollectionView 不重新加载数据
问题描述
我有一个 TableView,其中每个项目都有一个代码,当用户选择此表的任何项目时,它必须通过 HTTP-POST 在服务器上获取数据并将 CollectionView 加载到另一个视图中。
当这个新视图打开时,数据集合是空的,然后挤压一个线程以通过 HTTP-POST 获取数据,搜索正在工作,但在填充集合后数据不会出现在 CollectionView 中。
这是我的代码:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var root: UIView!
@IBOutlet weak var lbDescricao: UILabel!
@IBOutlet weak var colData: UICollectionView!
var desc: String = ""
var codigoCategoria: Int!
var listaProdutos: Array<Produto>!
var items: [IndexPath] = Array()
override func viewDidLoad() {
super.viewDidLoad()
self.doPostProdutos()
lbDescricao.text = self.desc
print( self.listaProdutos ) // Here it show “nil”
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return listaProdutos.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.items.append(indexPath)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionCellProduto
cell.imgProduto.image = UIImage( named: listaProdutos[indexPath.item].imagem )
cell.lbNome.text = listaProdutos[indexPath.item].nome
cell.lbPreco.text = "R$ " + String( listaProdutos[indexPath.item].preco )
return cell;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize( width: collectionViewSize/2, height: collectionViewSize/2 )
}
func doPostProdutos() {
let parameter = ["categoria":self.codigoCategoria]
let url = URL(string: "localhost/adm/api/produtos/produtosCategoria.php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let httpBody = try? JSONSerialization.data(withJSONObject: parameter, options: [])
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print( response )
}
if let data = data {
DispatchQueue.main.async{ // Thread
do {
self.listaProdutos = Array()
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]
for prod in json {
let nome = prod["nome"] as! String
let preco = (prod["preco"] as! NSString).doubleValue
let img = prod["imagem"] as! String
let produto: Produto = Produto(nome: nome, preco: preco, img: img)
self.listaProdutos.append(produto)
}
// Refresh
print( self.listaProdutos )
self.colData.reloadData()
self.colData.reloadItems(at: self.items)
}
catch {
print(error)
}
}
}
}.resume()
}
}
我究竟做错了什么?
解决方案
您需要将委托和数据源设置UICollectionView
为self
colData.delegate = self
colData.dataSource = self
你可以这样做viewDidLoad()
推荐阅读
- android - 卸载应用程序是否会删除应用程序使用的设备所有者设置?
- python - 如何重新启动kivy?
- c# - 解析日志文件,不明确的分隔符
- python - 使用 BeautifulSoup 解析却一无所获
- sql-server - 如何将文件和其他信息发布到控制器并将其保存在 sql server 中?
- python - 从一个整数列表到另一个整数列表的最小转换次数
- postgresql - 如何在 PostgreSQL 中锁定 SELECT?
- r - 当情节中有一些阿拉伯或波斯字母时,如何使用逆戟鲸保存情节?
- azure - 我的查询是否有任何问题以获取我的令牌?
- cluster-analysis - 查找 DBLP 数据集中的簇数