在我的iOS应用程序中有一个表格,其中一些单元格包含一个UITextField,我想在点击UITextField时选择其UIPiCkerView,在选择后UIPickerView应该消失。在this教程之后,我编写了以下类来表示带有选择器视图的单元格:
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
}
}
但我认为我遗漏了一些东西,因为当我点击UITextView时,UIPickerView没有出现,但光标显示在其中。我错过了什么?
答案 0 :(得分:0)
首先你的数据和uiviewController在哪里? 你的pickerView是正确的但你应用的地方?
尝试这样开始你的课程 class ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,CLLocationManagerDelegate
而不是"类PickerCell:UITableViewCell" 你使用" tableViewCell"创建一个选择器查看这不是一个好主意