我有一个包含可重复使用单元格的表格视图,每个单元格都有一个文本字段(例如篮子中的数量字段)。而且,每次编辑结束或用户在键盘外的某个地方点击时,我都需要更新一些数据,因此我同时使用
@IBAction func quantityEditingDidEnd(_ sender: UITextField) {
//code
}
@IBAction func quantityPrimaryActionTriggered(_ sender: UITextField) {
let cell = sender.superview?.superview as! BasketTableItemCell
let indexPath = tableView?.indexPath(for: cell)
//code
}
我在primaryActionTriggered
上出错,因为发件人应该是Any
,但是我需要使用UITextfield
来检测单击了哪个单元格。我该如何解决-使用primaryActionTriggered
并同时将发件人检测为UIView
?
*** UPD 我已经尝试过以这种方式更改代码-
@IBAction func quantityPrimaryActionTriggered(_ sender: Any) {
print("test")
if let textField = sender as? UITextField {
let cell = textField.superview?.superview as! BasketTableItemCell
let indexPath = tableView?.indexPath(for: cell)
//code
}
}
但是请注意,控制台上没有行"test"
。因此,我猜想这个@IBAction
根本不起作用。也许关键在于可重用单元中? didEndEditing
运作良好。也可以连接到StoryBoard。
答案 0 :(得分:0)
您可以将元素传递为Any类型,然后像这样强制转换
@IBAction func quantityPrimaryActionTriggered(_ sender: Any) {
if let textField = sender as? UITextField {
let cell = textField.superview?.superview as! BasketTableItemCell
let indexPath = tableView?.indexPath(for: cell)
//code
}
}