我已经将UITableViewCell子类化了。在这里,我为按钮做了一个出口。单击该按钮后,它应该在另一个ViewController中执行一个函数。我试过这个:
override func awakeFromNib() {
super.awakeFromNib()
reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touchUpInside)
}
但是,这会因此错误而崩溃:
[test.CreateChannelCell tappedOnReloadTableView]:无法识别的选择器发送到实例0x7fc1198b5200
该功能存在,没有拼写错误。为什么这不起作用?这个问题看起来一样,只是它不是用swift 3.0 How to set Action for UIButton in UITableViewCell
写的multiplayerChannelView是一个包含UITableView的viewcontroller。我有一个带有UITableViewCell子类的单独的.swift文件。
答案 0 :(得分:4)
在cellForRowAtIndexPath
中添加cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);
同一个UIVeiwController中的任何地方都定义了如下函数
func tappedButton(sender : UIButton){
// Your code here
}
答案 1 :(得分:1)
在文件VC2中写下代码
import UIKit
class tblCell : UITableViewCell
{
@IBOutlet weak var btnAction: UIButton!
@IBAction func btnAction(_ sender: AnyObject)
{
print(sender.tag) // you can identify your cell from sender.tag value
// notification is fire here and call notification from VC1
NotificationCenter.default.post(name:NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
}
class VC2: UIViewController,UITableViewDelegate,UITableViewDataSource
{
@IBOutlet weak var tblview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tblview.delegate = self
tblview.dataSource = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table veiw
func tableView(_ tblBlog: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tblBlog: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell : tblCell = tblview.dequeueReusableCell(withIdentifier: "Cell") as! tblCell
cell.btnAction.tag = indexPath.row
return cell
//
}
}
在文件VC1中写下代码
class VC1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool)
{
// this notification is call when it fire from VC2
NotificationCenter.default.addObserver(self, selector: #selector(ButtonClick), name: NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
func ButtonClick()
{
// code when button is clicked you wanto perfrom
}
}
答案 2 :(得分:1)
您可以在delegate
课程中创建tableViewCell
来完成此操作。
protocol CustomTableViewCellDelegate {
func buttonPressed ()
}
然后在tableViewCell
var delegate: CustomTableViewCellDelegate?
并将按钮操作放在tableViewCell
类
@IBAction func cellButtonPressed (sender : UIButton) {
if (self.delegate != nil) {
self.delegate?.buttonPressed()
}
点击按钮点击检查,代理人不是零,请在cell.delegate = self
方法中设置cellForRowAtIndex
在最后,只需在您使用customTableViewCell
类
extension ViewController : CustomTableViewCellDelegate {
func buttonPressed () {
// Perfom your code on button action
}
}
您的CustomTableViewCellDelegate
如下所示:
import UIKit
protocol CustomTableViewCellDelegate {
func buttonPressed ()
}
class CustomTableViewCell: UITableViewCell {
var delegate: CustomTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func cellButtonPressed (sender : UIButton) {
if (self.delegate != nil) {
self.delegate?.buttonPressed()
}
}
希望它适合你!