swift - swift - primaryActionTriggered - 发件人问题
问题描述
我有一个带有可重用单元格的表格视图,每个单元格都有一个文本字段(如篮子中的数量字段)。每次编辑结束或用户点击键盘外的某个地方时,我都需要更新一些数据,所以我同时使用
@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 的连接也可以。
解决方案
您可以将元素作为 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
}
}
推荐阅读
- django - 如何在 Q 对象中使用相关名称字段
- xaml - 基本控件(如 Button、TextBlock)是否具有 xaml 文件实现?
- javascript - 使用 canvas.blob 函数面临一些问题
- node.js - 使用 twilio nodejs 创建调查
- flutter - 搜索栏实现不起作用。代码不搜索任何东西
- r - R; 想要制作一个漂亮的分组条形图(ggplot2),y 轴上有 %,x 轴上有类别(总共 9 个),每个类别有四个分组条
- react-native - React-Native 渲染一个有 2 行的水平列表
- docker - 运行 Docker nginx 找不到复制的文件
- postgresql - sqlalchemy 无法在 for 循环中执行 session.add()
- bootstrap-table - 引导表不适用于 RouterLink