UIPickerView获取另一个UIPickerView的值

时间:2018-02-25 09:18:11

标签: ios swift uicollectionview uipickerview

我有多个UIPickerView的问题,我在UICollectionView上有8个,当我滚动第一个PickerView并且第五个PickerView开始自己滚动并获取第一个的相同值时,我滚动第二个PickerView和取第六个PickerView的相同值,依此类推。有这个问题的解决方案吗?谢谢你们。

以下是此问题的视频:Video

代码:

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

extension ViewController: UICollectionViewDataSource,          UICollectionViewDelegateFlowLayout {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 8
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = cardCollectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! Cards
    cell.productoTitle.text = productosTitles[indexPath.item]
    cell.backgroundCard.image = productosImages[indexPath.item]
    return cell
  }
}

0 个答案:

没有答案