Problem with collectionView

Asked

Viewed 62 times

0

Hello, good afternoon.

I’m facing a problem with the collectionView Swift, it just doesn’t show up, I don’t quite understand this issue, but it doesn’t get into the routine of the collectionView Cell, I don’t know if that’s really how it works either, so I would like to have your help, below I’ll put the code I’m using from the collectionView:

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


        //   let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CarCollectionViewCell
        //   cell.container.layer.backgroundColor = UIColor.lightGrayColor().CGColor

        //let cell = collectionView.cellForItem(at: indexPath) as! CarCollectionViewCell




        checkvaluecollectioncell = indexPath.row

        self.selectvalue = 1

        self.markers.removeAll()
        self.driverIds.removeAll()




       if(UserDefaults.standard.object(forKey: "PreferredLanguage") as! String == "en"){

            GlobalVarible.firstcarname = CarsTimedata.details![indexPath.row].carTypeName!
            GlobalVarible.cartypename = CarsTimedata.details![indexPath.row].carTypeName!
        }else{
            GlobalVarible.firstcarname = CarsTimedata.details![indexPath.row].carTypeName!
            GlobalVarible.cartypename = CarsTimedata.details![indexPath.row].carTypeName!
        }

        GlobalVarible.car_type_id = CarsTimedata.details![indexPath.row].carTypeId!


        GlobalVarible.cartypeid = CarsTimedata.details![indexPath.row].carTypeId!

        GlobalVarible.Cityid = CarsTimedata.details![indexPath.row].cityId!

        // aqui caminho imagem - 20/10/2018
        GlobalVarible.cartypeimage = CarsTimedata.details![indexPath.row].carTypeImage!


        if CarsTimedata.details![indexPath.row].rideMode == "2"{

            print("ridemode2")

            ApiManager.sharedInstance.protocolmain_Catagory = self
            ApiManager.sharedInstance.SelectRentalCar(CityID: GlobalVarible.Cityid)

        }else{
            self.mapview.clear()
             self.postdata.removeAll()
            self.datagetfromgeofire()
           //21/10/2018
            //self.GetDatafromfirebase()

        }

        MapCollectionview.reloadData()


    }

And I’ve got another routine from the collectionView Cell that’s below:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = MapCollectionview.dequeueReusableCell(withReuseIdentifier: "Mapcollectioncell", for: indexPath) as! CarCollectionViewCell



        cell.container.layer.shadowColor = UIColor.gray.cgColor
        cell.container.layer.shadowOpacity = 1
        cell.container.layer.shadowOffset = CGSize(width: 0, height: 2)
        cell.container.layer.shadowRadius = 2

          //  .addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

        let newString = CarsTimedata.details![indexPath.row].carTypeImage!
        print(newString)


        let url = imageUrl + newString


        let alerta = UIAlertController(
            title: "Falha de Comunicação",
            message: url,
            preferredStyle: .alert)

        present(alerta, animated: true, completion: nil)



        let url1 = NSURL(string: url)

        cell.carimage!.af_setImage(withURL:
            url1! as URL,
            placeholderImage: UIImage(named: "dress"),
            filter: nil,
            imageTransition: .crossDissolve(1.0))


        if(checkvaluecollectioncell == indexPath.row)
        {


           // cell.container.layer.backgroundColor =  (UIColor(red: 179.0/255.0, green: 191.0/255.0, blue: 191.0/255.0, alpha: 1.0) as! CGColor)
         //   cell.container.layer.backgroundColor = UIColor.lightGray.cgColor

            cell.container.backgroundColor = UIColor(red:147.0/255.0, green:165.0/255.0, blue:165.0/255.0, alpha:1.0)

            cell.carname.textColor = UIColor.white
            cell.cartime.textColor = UIColor.white
            //21/10/2018
             //cell.checkRadioBtn.image = UIImage(named: "Circled Dot-35 (1)")

        }else{

            cell.container.backgroundColor = UIColor.white
            cell.carname.textColor = UIColor(red:147.0/255.0, green:165.0/255.0, blue:165.0/255.0, alpha:1.0)
            cell.cartime.textColor = UIColor(red:243.0/255.0, green:156.0/255.0, blue:18.0/255.0, alpha:1.0)
            // cell.checkRadioBtn.image = UIImage(named: "Circle Thin-35 (1)")

        }


        cell.carname.text = CarsTimedata.details![indexPath.row].carTypeName!

        cell.cartime.text = CarsTimedata.details![indexPath.row].baseFare!


        return cell

    }


Eu chamo a rotina assim:

                let indexPathForFirstRow = IndexPath(row: 0, section: 0)
                self.collectionView(MapCollectionview, didSelectItemAt: indexPathForFirstRow)


Porem ele não entra na rotina do collectionView Cell, como falei não entendo muito bem como funciona então não sei se é assim mesmo o funcionamento, porem ele so entra na outra rotina do collectionView que é essa:

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

and consequently does not access the routine data that is in collectionView Cell

If anyone can help me it would be nice.

1 answer

0

In class definition, you must implement UICollectionViewDelegate and UICollectionViewDataSource and somewhere, for example in viewDidLoad collectionview.delegate = self

Also see if your collectionView has the correct constraints on the screen so that it is visible enough to show some item. If the collectionView is not set in width, maybe the method cellForItemAt don’t be called.

  • 1

    Whoa, man, I’ll see you here, thanks for the tip. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.