我试图使UIStackview成为反应性和声明性的,正如我在这里的问题中所回答的那样setting ids on item in Stackview,但我现在遇到的问题是,当我试图绑定到stackView时,我会得到这个错误Cannot convert value of type '(_) -> (@escaping (Int, _.Element, UIView?) -> UIView) -> Disposable' to expected argument type '(Observable<[LandMarkData]?>) -> (_) -> _'
下面是我使用的代码

extension Reactive where Base: UIStackView {

    func items<Sequence: Swift.Sequence, Source: ObservableType>(_ source: Source) -> (_ viewForRow: @escaping (Int, Sequence.Element, UIView?) -> UIView) -> Disposable where Source.E == Sequence {
        return { viewForRow in
            return source.subscribe { event in
                switch event {
                case .next(let values):
                    let views = self.base.arrangedSubviews
                    let viewsCount = views.count
                    var valuesCount = 0
                    for (index, value) in values.enumerated() {
                        if index < viewsCount {
                            // update views that already exist
                            _ = viewForRow(index, value, views[index])
                        }
                        else {
                            // add new views if needed
                            let view = viewForRow(index, value, nil)
                            self.base.addArrangedSubview(view)
                        }
                        valuesCount = index
                    }
                    if valuesCount + 1 < viewsCount {
                        for index in valuesCount + 1 ..< viewsCount {
                            // remove extra views if necessary
                            self.base.removeArrangedSubview(views[index])
                            views[index].removeFromSuperview()
                        }
                    }
                case .error(let error):
                    fatalError("Errors can't be allowed: \(error)")
                case .completed:
                    break
                }
            }
        }
    }
}

在我的视图控制器中
var features: [LandMarkData] = [LandMarkData]()
var selectedFeatures: [LandMarkData] = [LandMarkData]()

    let stackFeature = UIStackView()
    var stv: CheckboxStackView?
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        hideKeyboardWhenTappedAround()
        layout()
        logic()
        tableViewInit()

        self.stackFeature.axis = .vertical
        self.stackFeature.distribution = .fill
        self.stackFeature.spacing = 8

        let featured = NetworkAdapter.instance.getFeaturesAmeneities()
            .map { $0.data }
            .share(replay: 1)

        featured
            .bind(onNext: { [weak self] in self?.features.append(contentsOf: $0!) })
            .disposed(by: disposeBag)


        featured
            .bind(to: stackFeature.rx.items) { (row, element, view) in
                let myView = (view as? CheckboxStackView) ?? CheckboxStackView()

                myView.label.text = element.name
                myView.checkBox.tag = element.id ?? 0
                return myView
            }
            .disposed(by: disposeBag)

    }

任何帮助我为什么得到错误和修复是感激的

最佳答案

问题是在需要数组的地方传递可选数组。

关于ios - 无法在UIStackview响应式(Reactive)和声明式Swift中转换类型的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57655773/

10-10 21:05