gpt4 book ai didi

ios - UIPickerView 获取另一个 UIPickerView 的值

转载 作者:行者123 更新时间:2023-11-30 11:44:39 27 4
gpt4 key购买 nike

我有多个 UIPickerView 的问题,我在 UICollectionView 上有 8 个,当我滚动第一个 PickerView 和第五个 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
}
}

最佳答案

出现此问题的原因是您的单元被重复使用。解决问题的可能性很小:

  1. 不要重复使用单元格。这种方式对性能不利。

  2. 实现prepareForReuse()。在此方法中,您需要丢弃所有动画并清除单元格。

关于ios - UIPickerView 获取另一个 UIPickerView 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48972012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com