Problem with UIPickerView

0

I have a problem with 8 UIViewPickers, when moving the first UIPickerView the fifth UIPickerView is also moved without having moved it, taking the same value as the first one, if I move the second one moves the sixth one and so on, do you know how I can solve it?

I leave a video showing what happens: Video

The UIPickerView is added to a class of type UICollectionViewCell and added to a UICollectionView that is in the main ViewController. I leave the code where the PickerView is added:

    import UIKit

class Cards: UICollectionViewCell {

    var number: [String] = []
    let firaSansFont = FiraSansFont()

    let cardView: UIView = {
        let view = UIView()
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        view.layer.cornerRadius = 14
        view.layer.shadowOpacity = 0.20
        view.layer.shadowOffset = CGSize(width: 0, height: 10)
        view.layer.shadowRadius = 10
        view.frame.size = CGSize(width: 200, height: 200)
        return view
    }()

    let productoTitle: UILabel = {
        let attributes: [NSAttributedStringKey:Any] = [.foregroundColor : #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1)]
        let label = UILabel()
        label.attributedText = NSAttributedString(string: "Titulo", attributes: attributes)
        label.frame = CGRect(x: 16, y: 16, width: 200, height: 30)
        label.textAlignment = .left
        return label
    }()

    let backgroundCard: UIImageView = {
        let image = UIImageView(image: #imageLiteral(resourceName: "tortillaNormal"))
        image.contentMode = .scaleAspectFit
        image.layer.opacity = 0.5
        image.frame = CGRect(x: 20, y: 20, width: 160, height: 160)
        return image
    }()

    let numberPicker: UIPickerView = {
        let picker = UIPickerView()
        picker.frame = CGRect(x: 20, y: 70, width: 160, height: 100)
        return picker
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        layer.cornerRadius = 14
        layer.shadowOpacity = 0.20
        layer.shadowOffset = CGSize(width: 0, height: 10)
        layer.shadowRadius = 10
        addViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addViews() {
        for i in 0...100 { number.append(String(i)) }
        addSubview(cardView)
        addSubview(backgroundCard)
        productoTitle.font = firaSansFont.regular(size: 28)
        addSubview(productoTitle)
        numberPicker.dataSource = self
        numberPicker.delegate = self
        numberPicker.tag = instancia
        addSubview(numberPicker)
        let cantidadLabel: UILabel = {
            let label = UILabel()
            label.frame = CGRect(x: 0, y: 70, width: 200, height: 20)
            label.text = "Cantidad"
            label.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
            label.textAlignment = .center
            return label
        }()
        addSubview(cantidadLabel)
    }

}

extension Cards: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 100
    }


    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var pickerLabel: UILabel? = (view as? UILabel)
        if pickerLabel == nil {
            pickerLabel = UILabel()
            pickerLabel?.font = UIFont.systemFont(ofSize: 30, weight: .bold)
            pickerLabel?.textAlignment = .center
        }
        pickerLabel?.text = number[row]

        return pickerLabel!
    }

}
    
asked by Mario Castellano 23.02.2018 в 21:47
source

1 answer

1

You are using the same class as a delegate friend, but you are not controlling in the delegated method that UIPickerView refers to. You can use their property " tag " to distinguish them later in the delegated methods.

func numberOfComponents(in pickerView: UIPickerView) -> Int {
   switch pickerView.tag {
   case 0:
      acción para el 0
   case 1:
      acción para el 1
   etc...
   }
}
    
answered by 01.03.2018 / 12:28
source