Ios 如何在swift中为uitableview上的文本字段提供文本字段更改事件?

Ios 如何在swift中为uitableview上的文本字段提供文本字段更改事件?,ios,swift,uitableview,uitextfielddelegate,Ios,Swift,Uitableview,Uitextfielddelegate,我在每一行都有一个UITableView,它有一个文本字段。当用户在文本字段中键入某个内容时,我想触发该文本字段的didchange或其他内容。基本上,当文本字段改变时,我想计算一些东西,我该怎么做?我用的是swift 以下是我的部分代码: func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRo

我在每一行都有一个UITableView,它有一个文本字段。当用户在文本字段中键入某个内容时,我想触发该文本字段的didchange或其他内容。基本上,当文本字段改变时,我想计算一些东西,我该怎么做?我用的是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协议。有什么建议吗?

您必须区分操作方法和委托方法。委托方法的正确签名为。因此,您必须将其实现为:

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

    NSLog("Event Triggered")
    return true
}
您可以使用您的方法
textfielddichange(:)
作为操作方法。要注册它,请使用:

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

您必须区分action方法和delegate方法。委托方法的正确签名为。因此,您必须将其实现为:

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

    NSLog("Event Triggered")
    return true
}
您可以使用您的方法
textfielddichange(:)
作为操作方法。要注册它,请使用:

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

对于
UITextFieldDelegate
中的UITextField,没有像
textFieldDidChange
那样的方法。这就是为什么没有人叫你。您需要实现
UITextFieldDelegate
中提供的方法之一

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

对于
UITextFieldDelegate
中的UITextField,没有像
textFieldDidChange
那样的方法。这就是为什么没有人叫你。您需要实现
UITextFieldDelegate
中提供的方法之一

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

您将
textfield didchange
添加到哪个textfield中的CellForRowat中为什么不在
AddPriceCell
单元格类中添加委托方法?您的_textfield.addTarget(self,action:#选择器(textChanged(:)、for:.valueChanged)必须在自定义单元格类中编写委托方法。也在那里设置委托。@Jaydeep:不,这通常是不正确的。视图控制器也是代理的良好助手。如果未调用该方法,这至少不是因为viewcotroller是委托。您在CellForRow中的哪个文本字段中添加了
textFieldDidChange
,为什么不在
AddPriceCell
cell类中添加委托方法?您的\u textfield.addTarget(self,action:#选择器(textChanged(:)),for:.valueChanged)您必须在自定义单元格类中编写委托方法。也在那里设置委托。@Jaydeep:不,这通常是不正确的。视图控制器也是代理的良好助手。如果未调用该方法,这至少不是因为viewcotroller是委托。