OStack程序员社区-中国程序员成长平台

标题: ios - 如何将数据附加到 Collection View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:00
标题: ios - 如何将数据附加到 Collection View

下面有我的代码,它将来自 api 的数据显示到 Collection View 中。我想知道的是如何附加状态等于我们说的“已完成”的数据我只想在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 { (responseataResponse<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

            }
        }

    }

Collection View 的代码 -

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

        }



Best Answer-推荐答案


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
    }

关于ios - 如何将数据附加到 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49735620/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4