如何对我的 viewController 中的自定义 UITableViewCell 执行操作?
How to perform action for a custom UITableViewCell inside my viewController?
我有一个带有 xib 的自定义单元格,这个单元格包含一个按钮,按下按钮时我想执行一个操作,但不是在我的自定义单元格中 class 而是从 viewcontroller 包含自定义单元格的表格视图,请帮忙?
只需在您的 cellForRowAtIndexpath
中获取 UIButton
。
然后编写如下代码。
button.addTarget(self, action:#selector(buttonAction(_:)), for: .touchUpInside).
func buttonAction(sender: UIButton){
//...
}
从 viewController.
的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
方法中为按钮添加操作
cell.btn.tag = indexPath.row;
cell.btn.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside).
在您的视图控制器中编写选择器函数:
func handleButtonClicked(sender: UIButton){
int cellofClickedbutton = sender.tag;
//...
}
在您的单元格中定义协议:
protocol MyCellDelegate: class {
func buttonTapped()
}
并定义一个委托变量:
weak var delegate: MyCellDelegate?
让你的viewController
符合定义的协议。
在 viewController
中创建单元格时,为其分配委托:
cell.delegate = self
点击单元格中的按钮时,向委托人发送适当的消息:
delegate?.buttonTapped()
在您的 viewController
中实施方法:
func buttonTapped() {
//do whatever you need
}
您可以将单元格的索引作为参数传递,因此 viewController
知道点击了哪个单元格按钮。
首先你应该写一个协议,例如:
protocol CustomCellDelegate {
func doAnyAction(cell:CustomUITableViewCell)
}
然后在您的自定义单元格中 class 声明:
weak var delegate:CustomCellDelegate?
在自定义单元格中的 IBAction 中 class:
@IBAction func onButtonTapped(_ sender: UIButton) {
delegate?.doAnyAction(cell: self)
//here we say that the responsible class for this action is the one that implements this delegate and we pass the custom cell to it.
}
现在在您的 viewController 中:
1- 让您的视图控制器实现 CustomCellDelegate。
2- 在你的 cellForRow 中声明单元格时不要忘记写:
cell.delegate = self
3- 最后在 viewcontroller:
中调用函数
func doAnyAction(cell: CustomUITableViewCell) {
let row = cell.indexPath(for: cell)?.row
//do whatever you want
}
}
您可以使用委托模式。创建自定义协议。
protocol CustomTableViewCellDelegate {
func buttonTapped() }
以及 table查看单元格符合委托
class CustomTableViewCell: UITableViewCell {
var delegate: CustomTableViewCellDelegate!
@IBAction func buttonTapped(_ sender: UIButton) {
delegate.buttonTapped()
} }
table查看数据源
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CustomTableViewCell
cell.delegate = self
return cell
}
从table视图控制器或视图控制器
遵守协议(委托)
extension TestViewController: CustomTableViewCellDelegate {
func buttonTapped() {
print("do something...")
} }
我有一个带有 xib 的自定义单元格,这个单元格包含一个按钮,按下按钮时我想执行一个操作,但不是在我的自定义单元格中 class 而是从 viewcontroller 包含自定义单元格的表格视图,请帮忙?
只需在您的 cellForRowAtIndexpath
中获取 UIButton
。
然后编写如下代码。
button.addTarget(self, action:#selector(buttonAction(_:)), for: .touchUpInside).
func buttonAction(sender: UIButton){
//...
}
从 viewController.
的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
方法中为按钮添加操作
cell.btn.tag = indexPath.row;
cell.btn.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside).
在您的视图控制器中编写选择器函数:
func handleButtonClicked(sender: UIButton){
int cellofClickedbutton = sender.tag;
//...
}
在您的单元格中定义协议:
protocol MyCellDelegate: class {
func buttonTapped()
}
并定义一个委托变量:
weak var delegate: MyCellDelegate?
让你的viewController
符合定义的协议。
在 viewController
中创建单元格时,为其分配委托:
cell.delegate = self
点击单元格中的按钮时,向委托人发送适当的消息:
delegate?.buttonTapped()
在您的 viewController
中实施方法:
func buttonTapped() {
//do whatever you need
}
您可以将单元格的索引作为参数传递,因此 viewController
知道点击了哪个单元格按钮。
首先你应该写一个协议,例如:
protocol CustomCellDelegate {
func doAnyAction(cell:CustomUITableViewCell)
}
然后在您的自定义单元格中 class 声明:
weak var delegate:CustomCellDelegate?
在自定义单元格中的 IBAction 中 class:
@IBAction func onButtonTapped(_ sender: UIButton) {
delegate?.doAnyAction(cell: self)
//here we say that the responsible class for this action is the one that implements this delegate and we pass the custom cell to it.
}
现在在您的 viewController 中:
1- 让您的视图控制器实现 CustomCellDelegate。 2- 在你的 cellForRow 中声明单元格时不要忘记写:
cell.delegate = self
3- 最后在 viewcontroller:
中调用函数func doAnyAction(cell: CustomUITableViewCell) {
let row = cell.indexPath(for: cell)?.row
//do whatever you want
}
}
您可以使用委托模式。创建自定义协议。
protocol CustomTableViewCellDelegate {
func buttonTapped() }
以及 table查看单元格符合委托
class CustomTableViewCell: UITableViewCell {
var delegate: CustomTableViewCellDelegate!
@IBAction func buttonTapped(_ sender: UIButton) {
delegate.buttonTapped()
} }
table查看数据源
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! CustomTableViewCell
cell.delegate = self
return cell
}
从table视图控制器或视图控制器
遵守协议(委托)extension TestViewController: CustomTableViewCellDelegate {
func buttonTapped() {
print("do something...")
} }