菜鸟教程小白 发表于 2022-12-11 20:00:17

ios - 如何将数据附加到 Collection View


                                            <p><p>下面有我的代码,它将来自 api 的数据显示到 Collection View 中。我想知道的是如何附加状态等于我们说的“已完成”的数据我只想在collectionview上显示状态等于“已完成”的名称谢谢。或者例如 status = "created"。</p>

<p>示例 api 原始数据:</p>

<pre><code>         {
            &#34;id&#34;: 72,
            &#34;name&#34;: &#34;fsd&#34;,
            &#34;desc&#34;: &#34;fdsfsdf&#34;,
            &#34;reward&#34;: &#34;1.00&#34;,
            &#34;sched&#34;: &#34;2018-04-07T18:46:35.643713+08:00&#34;,
            &#34;parent&#34;: &#34;sfsd&#34;,
            &#34;child&#34;: &#34;fsdfdsf&#34;,
            &#34;occurrence&#34;: {
                &#34;name&#34;: &#34;once&#34;
            },
            &#34;status&#34;: {
                &#34;name&#34;: &#34;created&#34;
            },
            &#34;date_created&#34;: &#34;2018-04-09T21:15:16.263911+08:00&#34;,
            &#34;date_modified&#34;: &#34;2018-04-09T21:15:16.291715+08:00&#34;
      }
</code></pre>

<p>从 api 获取的代码</p>

<pre><code>func demoApi() {
      Alamofire.request(&#34;http://test.test:test/api/v1/chores/&#34;, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse&lt;Any&gt;) in

            switch(response.result) {


            case .success(_):
                guard let json = response.result.value as! []? else{ return}
                print(&#34;Response \(json)&#34;)
                for item in json {

                  self.getAllDetail.append(item

                }
                if !self.getAllDetail.isEmpty{
                  DispatchQueue.main.async {
                        self.collectionView.reloadData()
                  }
                }
                break

            case .failure(_):
                print(&#34;Error&#34;)
                break

            }
      }

    }
</code></pre>

<p> Collection View 的代码 -</p>

<pre><code>var getAllDetail: [] = []()

   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
            return getAllDetail.count
      }


      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {

         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &#34;Cell&#34;, for: indexPath) as! CollectionCell
            if let getTempDetails: = getAllDetail {
            cell.nameLabel.text = getTempDetails[&#34;name&#34;] as? String?? &#34;&#34; //titleArray
            }
            return cell
      }



      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

            // handle tap events
            print(&#34;You selected cell #\(indexPath.item)!&#34;)
            if let getTempDetails: = getAllDetail {
               print(&#34;You selected ID #\( getTempDetails[&#34;userId&#34;] as? String?? &#34;&#34; )!&#34;)
      }

      }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {

       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &#34;Cell&#34;, for: indexPath) as! CollectionCell
      if let getTempDetails: = getAllDetail {
            if let str = getTempDetails[&#34;status&#34;] as? {
                  if let name == str[&#34;name&#34;] as? String{
                     if name == &#34;created&#34;{

                     cell.nameLabel.text = getTempDetails[&#34;name&#34;] as! String

                   }else {
                     cell.nameLabel.text = @&#34;Uncreated&#34;

                  }
                }
             }
          }
      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]
查看完整版本: ios - 如何将数据附加到 Collection View