ios - 如何将数据附加到 Collection View
<p><p>下面有我的代码,它将来自 api 的数据显示到 Collection View 中。我想知道的是如何附加状态等于我们说的“已完成”的数据我只想在collectionview上显示状态等于“已完成”的名称谢谢。或者例如 status = "created"。</p>
<p>示例 api 原始数据:</p>
<pre><code> {
"id": 72,
"name": "fsd",
"desc": "fdsfsdf",
"reward": "1.00",
"sched": "2018-04-07T18:46:35.643713+08:00",
"parent": "sfsd",
"child": "fsdfdsf",
"occurrence": {
"name": "once"
},
"status": {
"name": "created"
},
"date_created": "2018-04-09T21:15:16.263911+08:00",
"date_modified": "2018-04-09T21:15:16.291715+08:00"
}
</code></pre>
<p>从 api 获取的代码</p>
<pre><code>func demoApi() {
Alamofire.request("http://test.test:test/api/v1/chores/", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
guard let json = response.result.value as! []? else{ return}
print("Response \(json)")
for item in json {
self.getAllDetail.append(item
}
if !self.getAllDetail.isEmpty{
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
break
case .failure(_):
print("Error")
break
}
}
}
</code></pre>
<p> Collection View 的代码 -</p>
<pre><code>var getAllDetail: [] = []()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return getAllDetail.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
if let getTempDetails: = getAllDetail {
cell.nameLabel.text = getTempDetails["name"] as? String?? "" //titleArray
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: = getAllDetail {
print("You selected ID #\( getTempDetails["userId"] as? String?? "" )!")
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
if let getTempDetails: = getAllDetail {
if let str = getTempDetails["status"] as? {
if let name == str["name"] as? String{
if name == "created"{
cell.nameLabel.text = getTempDetails["name"] as! String
}else {
cell.nameLabel.text = @"Uncreated"
}
}
}
}
return cell
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何将数据附加到 Collection View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49735620/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49735620/
</a>
</p>
页:
[1]