【问题标题】:Pulling a Textfield Value using a CustomCell File使用 CustomCell 文件提取文本字段值
【发布时间】: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


【解决方案1】:

只需将委托分配给它所在的单元格内的文本字段CustomCell

cell.entranceFeeTextField.delegate = self

但要确保cell 具有正确的标识符和正确的类

【讨论】:

  • cell.entranceFeeTextField.delegate 中的单元格到底是什么?在我的代码中我设置: let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as!String, for: indexPath) as!自定义单元
  • @Kevin 那么您是否将该委托设置为文本字段?
  • 当我将 cell.entranceFeeTextField.delegate = self 放入 cellForRowAtIndexPath 时出现错误
【解决方案2】:

您需要将选择器方法添加到您的文本字段,如下所示。

txtEntrance.addTarget(self, action:#selector(textFieldEndEditings(sender:)), for:.editingDidEnd)

func textFieldEndEditings(sender:UITextField) -> Void 
{
   print(sender.text)
}

【讨论】:

  • 我把这段代码放在哪个文件里?这个选择器方法有什么作用?
  • 这个选择器方法放在你的自定义类中。在您的文本字段结束编辑时调用此选择器。
  • 你也可以在你的 tableVies 的 cellForRowAtIndexPath 中添加这个选择器方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
相关资源
最近更新 更多