【问题标题】:How to give text field change event for text filed on uitableview in swift?如何在 swift 中为 tableview 上的文本字段提供文本字段更改事件?
【发布时间】:2017-08-10 05:54:50
【问题描述】:

我在每一行都有一个 UITableView,它有一个文本字段。当用户在文本字段上键入内容时,我想触发该 textFieldDidChange 或其他内容。基本上,当文本字段更改时,我想计算一些东西我该怎么做?我正在使用 swift。

这是我的部分代码:

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(tableView == self.seatsDropDownTblView){
        return seatsList!.count
    }else{
        return priceList!.count
    }

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if tableView == self.seatsDropDownTblView {
        let seats_cell = tableView.dequeueReusableCell(withIdentifier: "SeatsCell", for: indexPath) as! SeatsCell
        seats_cell.seatsCountLbl.text = self.seatsList?[indexPath.row]
        seats_cell.selectionStyle = .none
        return seats_cell

    }else {
        let price_cell = tableView.dequeueReusableCell(withIdentifier: "AddPriceCell", for: indexPath) as! AddPriceCell
        price_cell.txtPrice.text = priceList?[indexPath.row].Price
        price_cell.dropOffPointLbl.text = priceList?[indexPath.row].DropPoint

        self.indexPathArray.append(indexPath as NSIndexPath)
        price_cell.txtPrice.tag = indexPath.row
        //Delegates
        price_cell.txtPrice.delegate = self
        //Assign Delegates
        price_cell.txtPrice.delegate = self
        price_cell.selectionStyle = .none
        return price_cell

    }


}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView == self.seatsDropDownTblView {
        //let seats_cell = tableView.dequeueReusableCell(withIdentifier: "SeatsCell", for: indexPath) as! SeatsCell
        //seats_cell.selectionStyle = .none

        self.selectedSeat = (self.seatsList?[indexPath.row])!
        self.selectedSeatLbl.text = self.selectedSeat

        self.seatsDropDownView.isHidden = true
        isDropDownFalls = true
        self.dropDownBtnImg.image = UIImage(named:"drop_down")

    }else {
        let cell = tableView.cellForRow(at: indexPath) as! AddPriceCell
        cell.selectionStyle = .none

    }
}
func textFieldDidChange(_ textField: UITextField) {
    NSLog("Event Triggered")
}

这个委托没有被触发。我也导入了 UITextFieldDelegate 协议。有什么建议吗?

【问题讨论】:

  • 您在哪个文本字段中添加了textFieldDidChange in inside cellForRowAt
  • 为什么不在AddPriceCell单元类中添加委托方法?
  • YOUR_TEXTFILED.addTarget(self, action: #selector(textChanged(_:)), for: .valueChanged)
  • 您必须在自定义单元类中编写委托方法。还在那里设置委托。
  • @Jaydeep:不,这通常是不正确的。视图控制器也是代表的好候选人。如果没有调用该方法,这至少不是因为 viewcotroller 是委托。

标签: ios swift uitableview uitextfielddelegate


【解决方案1】:

你必须区分动作和委托方法。委托方法的正确签名是textField(_:, shouldChangeCharactersIn range:, replacementString:)。所以你必须将它实现为:

func textField(_ textField: UITextField, 
    shouldChangeCharactersIn range: NSRange, 
    replacementString string: String) -> Bool {

    NSLog("Event Triggered")
    return true
}

您可以使用您的方法textFieldDidChange(_:) 作为操作方法。要注册它,请使用:

price_cell.txtPrice.addTarget(self, 
    action: #selector(textFieldDidChange(_:)), 
    for: .valueChanged)

【讨论】:

    【解决方案2】:

    UITextFieldDelegate 中的 UITextField 没有像 textFieldDidChange 这样的方法。这就是为什么它没有被你召唤。您需要实现UITextFieldDelegate 中可用的方法之一。

    我猜textFieldDidEndEditing 是您可能想要实现的。 要检查 UITextFieldDelegate 中的所有方法,请在 XCode 中命令 + 单击 UITextFieldDelegate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2012-11-08
      • 2017-08-30
      相关资源
      最近更新 更多