Ios 如何将数据附加到集合视图
我在下面的代码中显示了从api到集合视图的数据。我想知道的是如何添加状态等于的数据,就像我们说的“已完成”一样。我只想在collectionview上显示状态等于“已完成”的名称。谢谢。或例如status=“created” api原始数据示例:Ios 如何将数据附加到集合视图,ios,swift,uicollectionview,httprequest,Ios,Swift,Uicollectionview,Httprequest,我在下面的代码中显示了从api到集合视图的数据。我想知道的是如何添加状态等于的数据,就像我们说的“已完成”一样。我只想在collectionview上显示状态等于“已完成”的名称。谢谢。或例如status=“created” api原始数据示例: { "id": 72, "name": "fsd", "desc": "fdsfsdf", "reward": "1.00",
{
"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"
}
从api获取的代码
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! [[String:Any]]? 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
}
}
}
您在这段代码中面临的确切问题是什么?我只想将状态等于createdcell.nameLabel.text=getTempDetails[“name”]的数据名显示为?一串“,我希望cell.name.label.text=getTempDetails[“name”]作为?字符串???”哪个状态等于createddoes上述代码能够检测到此名称是否具有此状态?是,请尝试。如果它仍然给出error than,请让我知道error msg。您的代码有问题,我实际想要的是,先生,我想要加载的数据的名称是状态等于created而不是name==created的数据,正如您在上面的api中看到的,有name和status,我认为您的代码只是确定name值是否等于createdShow name,哪个状态是created而不是name值
var getAllDetail: [[String:Any]] = [[String:Any]]()
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: [String : Any] = getAllDetail[indexPath.row] {
cell.nameLabel.text = getTempDetails["name"] as? String ?? "" //titleArray[indexPath.row]
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
print("You selected ID #\( getTempDetails["userId"] as? String ?? "" )!")
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
if let str = getTempDetails["status"] as? [String, String] {
if let name == str["name"] as? String{
if name == "created"{
cell.nameLabel.text = getTempDetails["name"] as! String
}else {
cell.nameLabel.text = @"Uncreated"
}
}
}
}
return cell
}