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.
99 lines
3.0 KiB
99 lines
3.0 KiB
// |
|
// DoubleTimePickerCell.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(12) |
|
static let topspace = kScaleHeight(15.0) |
|
} |
|
|
|
class DoubleTimePickerCell: TableViewCell { |
|
|
|
var selectedItems: [String]? { |
|
didSet { |
|
setSelectedItems() |
|
} |
|
} |
|
|
|
var type: Int = 0 |
|
var types: Int = 0 |
|
|
|
let begintimeLabel = UILabel().then { |
|
$0.textColor = Metric.textColor |
|
$0.font = Metric.textFont |
|
$0.textAlignment = .center |
|
} |
|
|
|
let endtimeLabel = UILabel().then { |
|
$0.textColor = Metric.textColor |
|
$0.font = Metric.textFont |
|
$0.textAlignment = .center |
|
} |
|
|
|
let pickerView = UIPickerView().then { _ in |
|
} |
|
|
|
typealias DoublePickerColsure = ([String]) -> Void |
|
var pickerColsure: DoublePickerColsure? |
|
|
|
override func makeUI() { |
|
super.makeUI() |
|
contentView.addSubview(begintimeLabel) |
|
contentView.addSubview(endtimeLabel) |
|
contentView.addSubview(pickerView) |
|
} |
|
|
|
override func layoutSubviews() { |
|
super.layoutSubviews() |
|
begintimeLabel.snp.makeConstraints { (make) in |
|
make.top.equalToSuperview().offset(Metric.topspace) |
|
make.left.equalToSuperview() |
|
make.right.equalTo(contentView.snp.centerX) |
|
} |
|
pickerView.snp.makeConstraints { (make) in |
|
make.top.equalToSuperview().offset(kScaleHeight(20.0)) |
|
make.left.right.equalToSuperview() |
|
make.bottom.equalToSuperview().offset(-kScaleHeight(10.0)) |
|
} |
|
endtimeLabel.snp.makeConstraints { (make) in |
|
make.top.equalToSuperview().offset(Metric.topspace) |
|
make.right.equalToSuperview() |
|
make.left.equalTo(contentView.snp.centerX) |
|
} |
|
} |
|
} |
|
|
|
extension DoubleTimePickerCell { |
|
func setSelectedItems() { |
|
let viewModel = DoublePickerViewModels(type: type,types:types) |
|
viewModel.dataSourceVariable.asObservable() |
|
.bind(to: pickerView.rx.items(adapter: DoublePickerViewViewAdapter())) |
|
.disposed(by: cellDisposeBag) |
|
pickerView.rx.modelSelected(String.self) |
|
.subscribe(onNext: { [weak self] items in |
|
guard let `self` = self else { return } |
|
if let picker = self.pickerColsure { |
|
picker(items) |
|
} |
|
}) |
|
.disposed(by: cellDisposeBag) |
|
if selectedItems?.count == 2 { |
|
if let first = selectedItems?.first, let index1 = viewModel.dataSourceVariable.value.first?.firstIndex(of: first) { |
|
pickerView.selectRow(index1, inComponent: 0, animated: false) |
|
} |
|
if let last = selectedItems?.last, let index2 = viewModel.dataSourceVariable.value.last?.lastIndex(of: last) { |
|
pickerView.selectRow(index2, inComponent: 1, animated: false) |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|