我有一個UITableView每行都有一個文本字段。當用戶在文本字段上鍵入內容時,我想觸發textFieldDidChange或其他任何內容。基本上當文本字段發生變化時,我想要計算一些東西,我該怎麼做呢?我正在使用swift。如何在swift中爲uitableview提供的文本字段更改事件?
這裏是我的代碼部分:
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協議。任何建議?
其中textfield是你在cellForRowAt中添加'textFieldDidChange' –
爲什麼不在'AddPriceCell'單元類中添加委託方法? – Akhilrajtr
YOUR_TEXTFILED.addTarget(self,action:#selector(textChanged(_ :)),for:.valueChanged) –