【发布时间】:2016-10-13 03:50:44
【问题描述】:
我有一个子类CustomCell,它继承自我的父类CreateEvent。该子类描述了表格视图单元格的各个单元格,它位于 CreateEvent 视图控制器上。在一个特定的单元格中,我有一个文本字段,它链接到 CustomCell 文件,但是当用户输入文本字段时,我无法从该文本字段中获取值。我也无法通过外部触摸关闭键盘并按返回键,但我主要专注于从文本字段中获取文本。我熟悉在一个普通的 swift 文件上执行这些功能,但因为这是一个子类和两个 swift 文件,我不知道该怎么做。这是我的代码:
class CustomCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var entranceFeeTextField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
和
class CreateEventVC: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate, UITextFieldDelegate {
override func viewDidLoad() {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell
// the following code describes the array for an expandable drop down
if currentCellDescriptor["cellIdentifier"] as! String == "idCellNormal" {
if let primaryTitle = currentCellDescriptor["primaryTitle"] {
cell.textLabel?.text = primaryTitle as? String
}
eventType = ((cellDescriptors[0] as! NSMutableArray)[0] as! NSDictionary)["primaryTitle"]! as! String
if let secondaryTitle = currentCellDescriptor["secondaryTitle"] {
cell.detailTextLabel?.text = secondaryTitle as? String
}
}
else if currentCellDescriptor["cellIdentifier"] as! String == "idCellTextfield" {
cell.textField.placeholder = currentCellDescriptor["primaryTitle"] as? String
}
else if currentCellDescriptor["cellIdentifier"] as! String == "idCellValuePicker" {
cell.textLabel?.text = currentCellDescriptor["primaryTitle"] as? String
}
cell.delegate = self
return cell
}
func textFieldShouldEndEditing(textField:UITextField) -> Bool {
entranceFeeAmount = cell.entranceFeeTextField.text!
return true;
}
}
我的主要问题是我是否正确地符合 UITextField Delegates,因为我无法打印出 entryFeeTextField 的值。我无法得到解决我的问题的答案,或者我能理解的答案。
【问题讨论】:
-
在textFieldShouldEndEditing方法中直接写'entryFeeAmount = textField.text'
-
您需要将 UITextField 的委托设置为 cellForRowAt 中的当前视图控制器,以使用当前视图控制器中的委托方法。
-
@yagneshdobariya 我不能这样做,因为我没有将 IBOutlet 设置为 CreateEventVC,因此该行代码将无法识别 entryFeeTextField。我已将其链接到 CustomCell 文件
-
@AnkitaShah 我是 xcode swift 的新手。你能进一步解释一下吗?我需要放 cell.textField.delegate = self 吗?
-
在 cellForRowAtIndexPath 方法中添加 cell.entranceFeeTextField.delegate = self
标签: ios swift uitableview textfield