我没有使用默认的UIPickerView,请不要将其标记为重复。
我正在使用此库轻松创建表单:https://github.com/ortuman/SwiftForms
我希望在用户选择值后隐藏选择器视图。我试过了:
pickerView.hidden = true
但我仍然会在表单下面看到一个灰色栏:
以下是自定义控件:
//
// FormPickerCell.swift
// SwiftForms
//
// Created by Miguel Angel Ortuno on 22/08/14.
// Copyright (c) 2014 Miguel Angel Ortuño. All rights reserved.
//
import UIKit
public class FormPickerCell: FormValueCell, UIPickerViewDelegate, UIPickerViewDataSource {
// MARK: Properties
private let picker = UIPickerView()
private let hiddenTextField = UITextField(frame: CGRectZero)
// MARK: FormBaseCell
public override func configure() {
super.configure()
accessoryType = .None
picker.delegate = self
picker.dataSource = self
hiddenTextField.inputView = picker
contentView.addSubview(hiddenTextField)
}
public override func update() {
super.update()
picker.reloadAllComponents()
titleLabel.text = rowDescriptor.title
if let value = rowDescriptor.value {
valueLabel.text = rowDescriptor.titleForOptionValue(value)
if let options = rowDescriptor.configuration[FormRowDescriptor.Configuration.Options] as? NSArray {
let index = options.indexOfObject(value)
if index != NSNotFound {
picker.selectRow(index, inComponent: 0, animated: false)
}
}
}
}
public override class func formViewController(formViewController: FormViewController, didSelectRow selectedRow: FormBaseCell) {
if selectedRow.rowDescriptor.value == nil {
if let row = selectedRow as? FormPickerCell {
let options = selectedRow.rowDescriptor.configuration[FormRowDescriptor.Configuration.Options] as? NSArray
let optionValue = options?[0] as? NSObject
selectedRow.rowDescriptor.value = optionValue
row.valueLabel.text = selectedRow.rowDescriptor.titleForOptionValue(optionValue!)
row.hiddenTextField.becomeFirstResponder()
}
} else {
if let row = selectedRow as? FormPickerCell {
guard let optionValue = selectedRow.rowDescriptor.value else { return }
row.valueLabel.text = selectedRow.rowDescriptor.titleForOptionValue(optionValue)
row.hiddenTextField.becomeFirstResponder()
}
}
}
// MARK: UIPickerViewDelegate
public func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return rowDescriptor.titleForOptionAtIndex(row)
}
public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let options = rowDescriptor.configuration[FormRowDescriptor.Configuration.Options] as? NSArray
let optionValue = options?[row] as? NSObject
rowDescriptor.value = optionValue
valueLabel.text = rowDescriptor.titleForOptionValue(optionValue!)
pickerView.endEditing(true)
pickerView.hidden = true
}
// MARK: UIPickerViewDataSource
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if let options = rowDescriptor.configuration[FormRowDescriptor.Configuration.Options] as? NSArray {
return options.count
}
return 0
}
}