0

tengo un problema con 8 UIViewPickers, al mover el primer UIPickerView también se mueve el quinto UIPickerView sin haberlo movido, tomando el mismo valor que el primero, si muevo el segundo se mueve el sexto y así sucesivamente, saben como puedo solucionarlo?

Dejo un video mostrando lo que pasa: Video

El UIPickerView se agrega a una clase de tipo UICollectionViewCell y se agrega a un UICollectionView que esta en ViewController principal. Dejo el código donde se agrega el PickerView:

    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!
    }

}

1 respuesta 1

1

Estás usando la misma clase como delegado amigo, pero no estás controlando en el método delegado a que UIPickerView te refieres. Puedes usar su propiedad "tag" para distinguirlos luego en los métodos delegados.

func numberOfComponents(in pickerView: UIPickerView) -> Int {
   switch pickerView.tag {
   case 0:
      acción para el 0
   case 1:
      acción para el 1
   etc...
   }
}
4

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.