Uipickerview采用另一个Uipickerview的值



我有多个uipickerview的问题,我在uicollectionview上有8个,当我滚动第一个pickerview时,第五pickerview开始自行滚动并获得第一个相同的值,然后我滚动第二个Pickerview,并获得第六个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
        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
  }
}

这个问题发生了,因为您的单元格被重复使用。解决问题的可能性很少:

  1. 请勿重复使用细胞。这种方式对性能不利。

  2. 实施PreparforReuse((。在这种方法中,您需要丢弃所有动画并清除单元格。