lundi 30 août 2021

Collectionview going to if condition but not being called else condition in Swift

I am using two collectionviews and calling them according to conditions

Initially, I want to show newCollectionview with newArray images.

code for collectionviews: with this code isEdit condition working perfectly.

But initially when I upload images from picker then in cellForItemAt else condition not being called and added images in newArray are also not showing in newCollectionView.

but when I coming from isEdit then if collectionView == newCollectionView is working but if it's not from isEdit then it's not calling, why?

Here numberOfItemsInSection is also calling.. but images not showing in row

struct ImagesModel{

public var image : String?
init(image: String?) {
    self.image = image
}
}


import UIKit

class BidPlaceVC: UIViewController, UITextViewDelegate {

var oldArray = [ImagesModel]()
var newArray = [UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    let layout = UICollectionViewFlowLayout()
    
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    
    layout.scrollDirection = .horizontal
    let width = UIScreen.main.bounds.width/4.1
    let height = width*1.1
    
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 5
    layout.itemSize = CGSize(width: width, height: 100)

    self.newCollectionView.collectionViewLayout = layout
    
    self.oldCollectionnview.collectionViewLayout = layout

    newCollectionView.reloadData()
    oldCollectionnview.reloadData()
 }
 
}

extension BidPlaceVC : UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if isEdit {// here all conditions working
        if newArray.isEmpty{

        if collectionView == oldCollectionnview{

        return  oldArray.count
        }
        }
        else{
            if collectionView == oldCollectionnview{

            return  oldArray.count
            }
            if collectionView == newCollectionView{

            return  self.newArray.count
            }
        }
        
    }else {
        if collectionView == newCollectionView{

        return  self.newArray.count// is calling
        }
    }
    return newArray.count
 }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if isEdit{
        
        if newArray.isEmpty{
            if collectionView == oldCollectionnview{

              let cell = oldCollectionnview.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell

               let img = "\(CommonUrl.bidsAttachment)\( self.oldArray[indexPath.item].image ?? "")"
               cell?.imgView.getImage(withUrl: img, placeHolder: #imageLiteral(resourceName: "home"), imgContentMode: .scaleAspectFill)
                return cell!
            }

        }
        else{
            
            if collectionView == oldCollectionnview{

                let cell = oldCollectionnview.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell

               let img = "\(CommonUrl.bidsAttachment)\( self.oldArray[indexPath.item].image ?? "")"
               print("\(img)")
               cell?.imgView.getImage(withUrl: img, placeHolder: #imageLiteral(resourceName: "home"), imgContentMode: .scaleAspectFill)
                   return cell!
            }
             // if i come from isEdit then its calling.. and images showing in newCollectionView
            if collectionView == newCollectionView{

               let cell = newCollectionView.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell

                cell?.imgView.image = self.newArray[indexPath.item]
              
                return cell!
                
            }
            
        }
      
        }
        //******* here is not being called ******
       else{
        
        if collectionView == newCollectionView{

           let cell = newCollectionView.dequeueReusableCell(withReuseIdentifier: "FilesCollectionCell", for: indexPath) as? FilesCollectionCell
            print("first time \(self.newArray)")
            cell?.imgView.image = self.newArray[indexPath.item]
            
            return cell!
        }
    }
    return UICollectionViewCell()

}

}


extension BidPlaceVC : EasyImagePickerDelegate{

func didSelect(image: UIImage?, video: URL?, fileName: String?) {
    if let img = image{
        self.imageProfile = img
    
        self.newArray.append(img)
        print("added images \(newArray)")
        self.newCollectionView.reloadData()
    }
}
}

please do help to solve this issue

Aucun commentaire:

Enregistrer un commentaire