Ios 无法打印collectionView中api的响应
我有post请求类型API,我使用request.HTTP method=“post”点击API 这是我的密码Ios 无法打印collectionView中api的响应,ios,swift,Ios,Swift,我有post请求类型API,我使用request.HTTP method=“post”点击API 这是我的密码 var posts = [[String: Any]]() func apicall() { let Url = String(format: "http://example.com") guard let serviceUrl = URL(string: Url) else { return } var request = UR
var posts = [[String: Any]]()
func apicall() {
let Url = String(format: "http://example.com")
guard let serviceUrl = URL(string: Url) else { return }
var request = URLRequest(url: serviceUrl)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] {
self.posts = json["data"] as! [[String : Any]]
print("here the data that i want \(self.posts)")
}
} catch {
print(error)
}
}
}.resume()
}
现在我得到了JSON格式的响应并打印出来。现在我不知道如何在collectionView中打印JSON
这是我的collectionView代码
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CreatGroupCollectionViewCell
let link = "http://example.com" + posts[indexPath.row]["icons"] as! String
cell.lbl.text = posts[indexPath.row]["name"] as? String
cell.btn2.downloaded(from: link)
return cell
}
我无法在按钮上打印图像,我为此使用扩展名您只需重新加载collectionView-在do语句中尝试此操作
DispatchQueue.main.async() {
self.collectionView.reloadData()
}
你是说填充集合视图?检查基本内容“在collectionView中打印JSON”是什么意思?是否要在集合视图的单元格中表示JSON?您知道如何创建集合视图吗?到目前为止,您尝试了什么?@jnpdx我打印了我从集合中的API获得的jsonview@coceki因此,您希望在集合视图中显示
print
时打印到控制台的相同文本吗?全部在一个单元格中?@jnpdx是的,你是对的