【发布时间】: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 协议。有什么建议吗?
【问题讨论】:
-
您在哪个文本字段中添加了
textFieldDidChangein inside cellForRowAt -
为什么不在
AddPriceCell单元类中添加委托方法? -
YOUR_TEXTFILED.addTarget(self, action: #selector(textChanged(_:)), for: .valueChanged)
-
您必须在自定义单元类中编写委托方法。还在那里设置委托。
-
@Jaydeep:不,这通常是不正确的。视图控制器也是代表的好候选人。如果没有调用该方法,这至少不是因为 viewcotroller 是委托。
标签: ios swift uitableview uitextfielddelegate