如何将数据附加到集合视图

时间:2018-04-09 14:40:44

标签: ios swift uicollectionview httprequest

我的下面的代码显示了api中的数据到集合视图中。我想知道的是如何追加状态等于我们说“已完成”的数据我只想在collectionview上显示哪个状态等于“已完成”谢谢。或者例如status =“created”。

示例api原始数据:

         {
            "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

            }
        }

    }

集合视图的代码 -

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  ?? "" )!")
        }

        }

1 个答案:

答案 0 :(得分:0)

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
    }