Reputation: 5427
In My iOS app there is a table where some cells contain a UITextField and I would like when that UITextField is tapped then its UIPiCkerView is selected and, after the selection the UIPickerView should disappear. Following this tutorial I have written the following class representing a cell with a picker view:
import Foundation
import UIKit
class PickerCell: UITableViewCell, UIPickerViewDataSource,UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var label : UILabel!
@IBOutlet var myPicker: UIPickerView! = UIPickerView()
@IBOutlet weak var selectedData: UITextField!
var pickerData:[String]!
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedData.text = self.pickerData[row]
self.myPicker.hidden = true;
}
func configurePickerCell(labelText:String, enabled:Bool, defaultValueIndex:Int) {
self.label.text = labelText
var labelFrame:CGRect = self.label.frame
labelFrame.size.height = Settings.labelHeight
self.label.frame = labelFrame
self.myPicker.delegate = self
self.myPicker.dataSource = self
self.selectedData.delegate = self
self.setEditable(enabled)
}
func setEditable(canEdit:Bool) {
if (canEdit) {
self.selectedData.userInteractionEnabled = true
self.label.highlighted = false
self.selectedData.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).CGColor
}
else {
self.selectedData.userInteractionEnabled = false
self.label.highlighted = true
self.selectedData.layer.borderColor = UIColor(red: 176/255, green: 176/255, blue: 176/255, alpha: 1).CGColor
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
self.myPicker.hidden = false
return false
}
}
but I think I am missing something because when I tap on the UITextView the UIPickerView does not appear but the cursor is showed inside it. What am I missing?
Upvotes: 1
Views: 1798
Reputation: 169
so first where is your data and the uiviewController ? you pickerView is correct but where you appli ?
try start like this your class class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource , CLLocationManagerDelegate
and not "class PickerCell: UITableViewCell" you use a "tableViewCell" to create a pickerView this is not a good idea
Upvotes: 0