首页 > 解决方案 > 在coolectionView的数组中添加值的问题 - Swift

问题描述

我从数据库中提取数据。我正在根据 CATEGORIDAVTOPLAM.count 的数量分离我获取的数据。例如,第一节有 4 个数据,第二节有 3 个数据。但是,虽然 2.section 中的数据应该是新 DavFile 数组中的 5.6.7 项,但 Array 中应该是 1.2.7。吸引下一个数据。如何通过在单个目录中的部分中计数来添加数据?当我这样做时,我总共有7个数据如下。Array中第一节的排名是1.2.3.4,第二节的排名是1.2.7。价值观。

   func getIndexForArray(with indexPath:IndexPath)->Int{
        var itemIndex = 0
          if indexPath.row != 0{
              for _ in 0..<indexPath.section{
                  itemIndex += (sonsuzCollec.numberOfItems(inSection: indexPath.section) - 1)
              }
              return (itemIndex + indexPath.row)
          }else{
              return indexPath.row
          }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
           return  Int(KATEGORIDAVTOPLAM[section]) ?? 0 }
     func numberOfSections(in collectionView: UICollectionView) -> Int {
        return yeniDavKATIsımNew.count }
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellSonsuz", for: indexPath) as! sonsuzCell
  let currentIndex = getIndexForArray(with: indexPath)
              if currentIndex < yeniDavFile.count{
                 let urlNew = URL(string: yeniDavFile[currentIndex])
                 cell.davetiyeFoto.sd_setImage(with: urlNew)       
                 return cell
             }else{
                return UICollectionViewCell()
             }

 @objc func kategoriSaydırNew(){
...
             if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
                     for review in baslik {
                        if let soru_baslik = review["davetiyefilee"] as? String {
                              let s = String(describing: soru_baslik)
                               self.yeniDavFile.append(s)
                    } }  
                         DispatchQueue.main.async { self.sonsuzCollec.reloadData() }}
         if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
                            for review in baslik {
                            if let soru_baslik = review["TOPLAM"] as? Int {
                            let s = String(describing: soru_baslik)
                                self.KATEGORIDAVTOPLAM.append(s)


                                } }
                    DispatchQueue.main.async {
                                                   self.sonsuzCollec.reloadData()
                                                                         }

标签: swift

解决方案


推荐阅读