gpt4 book ai didi

ios - Swift:点击 UITextField 时如何显示 UIPickerView

转载 作者:行者123 更新时间:2023-11-28 08:57:57 24 4
gpt4 key购买 nike

在我的 iOS 应用程序中有一个表,其中一些单元格包含一个 UITextField,我希望当该 UITextField 被点击时,它的 UIPiCkerView 被选中,并且在选择之后 UIPickerView 应该消失。正在关注this教程我编写了以下类,表示具有选择器 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
}


}

但我想我遗漏了一些东西,因为当我点击 UITextView 时,UIPickerView 没有出现,但光标显示在其中。我错过了什么?

最佳答案

首先,您的数据和 uiviewController 在哪里?你的 pickerView 是正确的,但你在哪里应用?

试着这样开始你的课类 ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource , CLLocationManagerDelegate

而不是“类 PickerCell:UITableViewCell”你使用“tableViewCell”来创建一个 pickerView 这不是一个好主意

关于ios - Swift:点击 UITextField 时如何显示 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com