You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

102 lines
2.9 KiB

//
// SinglePickerCell.swift
// Lookfit
//
// Created by lemo on 2018/5/11.
// Copyright © 2020 ecell. All rights reserved.
//
import UIKit
fileprivate struct Metric {
static let textColor = kHexColor(0x2A2A2A)
static let textFont = SystemLightFont(14)
static let topspace = kScaleHeight(15.0)
}
class SinglePickerCell: TableViewCell {
var functionType: FunctionType? {
didSet {
loadFunctionType()
}
}
var selectedItem: String? {
didSet {
selectItem()
}
}
let titleLabel = UILabel().then {
$0.textColor = Metric.textColor
$0.font = Metric.textFont
$0.textAlignment = .center
}
let pickerView = UIPickerView().then { _ in
}
typealias SinglePickerClosure = (String) -> Void
var singlePickerClosure: SinglePickerClosure?
let subject = PublishSubject<String>()
var viewModel = SinglePickerViewModels()
override func makeUI() {
super.makeUI()
contentView.addSubview(titleLabel)
contentView.addSubview(pickerView)
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(Metric.topspace)
make.left.right.equalToSuperview()
}
pickerView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(kScaleHeight(20.0))
make.left.right.equalToSuperview()
make.bottom.equalToSuperview().offset(-kScaleHeight(10.0))
}
}
}
extension SinglePickerCell {
func selectItem() {
if selectedItem?.count == 1 {
if let idx = viewModel.dataSourceVariable.value.firstIndex(of: selectedItem!) {
pickerView.selectRow(idx, inComponent: 0, animated: false)
}
}
if let selectedItem = self.selectedItem {
if let idx = viewModel.dataSourceVariable.value.firstIndex(of: selectedItem) {
pickerView.selectRow(idx, inComponent: 0, animated: false)
}
}else {
pickerView.selectRow(1, inComponent: 0, animated: false)
}
}
func loadFunctionType() {
viewModel = SinglePickerViewModels(funtionType: functionType!)
viewModel.dataSourceVariable.asObservable()
.bind(to: pickerView.rx.items(adapter: SinglePickerViewViewAdapter()))
.disposed(by: cellDisposeBag)
pickerView.rx.modelSelected(String.self)
.subscribe(onNext: { [weak self] item in
guard let `self` = self else { return }
if let singlePicker = self.singlePickerClosure {
singlePicker(item.first!)
}
self.subject.onNext(item.first!)
})
.disposed(by: cellDisposeBag)
}
}