热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何对我的viewController中的自定义UITableViewCell执行操作?

如何解决《如何对我的viewController中的自定义UITableViewCell执行操作?》经验,为你挑选了1个好方法。

我有一个具有xib的自定义单元格,该单元格包含一个按钮,当按下按钮时,我想执行一个操作,但不是在我的自定义单元格类内部,而是从包含自定义单元格的viewviewler的内部请帮忙?



1> Jaafar Barek..:

首先,您应该编写一个协议,例如:

protocol CustomCellDelegate {
  func doAnyAction(cell:CustomUITableViewCell)
}

然后在您的自定义单元格类中声明:

weak var delegate:CustomCellDelegate?

并在自定义单元类的IBAction中:

@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

    }
}


推荐阅读
author-avatar
somnus
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有