我在每一行都有一个 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")
}
此委托(delegate)未被触发。我也导入了 UITextFieldDelegate 协议(protocol)。有什么建议吗?
最佳答案
您必须区分操作方法和委托(delegate)方法。委托(delegate)方法的正确签名是 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)
关于ios - 如何快速为 uitableview 上提交的文本提供文本字段更改事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605509/