gpt4 book ai didi

swift - 设置委托(delegate)时 RxSwift UIPicker 崩溃

转载 作者:行者123 更新时间:2023-11-28 15:12:08 28 4
gpt4 key购买 nike

于是我为我的app设置了一个uipicker,代码如下

self.leftPicker.rx.setDelegate(self).disposed(by: bag)
Driver.just([1, 2, 3, 4, 5, 6])
.drive(leftPicker.rx.items){
index, item, view in
if let label = view as? UILabel{
return label
}
let label = UILabel()
label.font = UIFont(name: SRFonts.asap_regular.fontName, size: 13)
label.textAlignment = .center
label.text = "\(item)"
return label
}
.disposed(by: bag)

如果我这样做,应用程序会崩溃,说有一个冲突的委托(delegate)......所以如果我要删除

self.leftPicker.rx.setDelegate(self).disposed(by: bag)

崩溃消失了...

但问题是,我想为 uipicker 行设置高度,没有委托(delegate),我似乎无法在

中设置高度
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 44
}

这真的很奇怪,因为它通常适用于 RxTableView 和 RxCollectionView。

有谁知道是怎么回事吗?

最佳答案

立即引起我注意的是,您传递的是数组而不是数组的数组。请尝试使用 [[1,2,3,4,5,6]]

let data: PickerViewAdapter.Element = [
["0", "1", "2"],
["a", "b", "c"]
]

Observable.just(data)
.bind(to: pickerView.rx.items(adapter: PickerViewAdapter()))
.disposed(by: self.disposeBag)

final class PickerViewAdapter: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType
, SectionedViewDataSourceType {

typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func model(at indexPath: IndexPath) throws -> Any {
return items[indexPath.section][indexPath.row]
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

/* ... */

编辑:

此外,在我的示例中,适配器是委托(delegate)。如果您不指定一个,它将自动创建委托(delegate)(请参阅 https://github.com/ReactiveX/RxSwift/blob/master/RxCocoa/iOS/UIPickerView%2BRx.swift ),因此会发生冲突。

使用您的自定义适配器类尝试 .bind(to: pickerView.rx.items(adapter: PickerViewAdapter()))

关于swift - 设置委托(delegate)时 RxSwift UIPicker 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47450069/

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