Ios 自定义单元格中的按钮操作不调用委托函数

Ios 自定义单元格中的按钮操作不调用委托函数,ios,swift,xcode,Ios,Swift,Xcode,我在视图的文件所有者中使用此代码,该视图在我的表视图中用作自定义单元格: import UIKit protocol PostCellViewDelegate: class { func postSettingsAction() } class PostCellView: UITableViewCell { weak var delegate: PostCellViewDelegate? @IBAction func postSettingsClicked(_

我在视图的文件所有者中使用此代码,该视图在我的表视图中用作自定义单元格:

import UIKit
protocol PostCellViewDelegate: class {
    func postSettingsAction()
}

class PostCellView: UITableViewCell {
    weak var delegate:  PostCellViewDelegate?



    @IBAction func postSettingsClicked(_ sender: UIButton) {
        delegate?.postSettingsAction()
        print("here2")
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let _ = commonInitialization()

    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        let _ = commonInitialization()

    }


    func customise(imageName : String , color : UIColor, logoLabelValue : String, websiteValue: String)
    {
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "PostCellView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view

    }

}
import UIKit

class ViewController: UIViewController, PostCellViewDelegate, UITableViewDataSource, UITableViewDelegate  {
    func postSettingsAction() {
        print("hi there")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = PostCellView(style: UITableViewCellStyle.default , reuseIdentifier: "Cell")

        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }


    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
在具有表视图的视图控制器中:

import UIKit
protocol PostCellViewDelegate: class {
    func postSettingsAction()
}

class PostCellView: UITableViewCell {
    weak var delegate:  PostCellViewDelegate?



    @IBAction func postSettingsClicked(_ sender: UIButton) {
        delegate?.postSettingsAction()
        print("here2")
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let _ = commonInitialization()

    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        let _ = commonInitialization()

    }


    func customise(imageName : String , color : UIColor, logoLabelValue : String, websiteValue: String)
    {
    }

    func commonInitialization() -> UIView
    {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "PostCellView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        addSubview(view)
        return view

    }

}
import UIKit

class ViewController: UIViewController, PostCellViewDelegate, UITableViewDataSource, UITableViewDelegate  {
    func postSettingsAction() {
        print("hi there")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = PostCellView(style: UITableViewCellStyle.default , reuseIdentifier: "Cell")

        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }


    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

正如在单击按钮时所预期的那样,代理应该调用postSettingsAction(),但是这不会发生。这里可能有什么问题,我应该如何纠正?

您尚未将代理分配给视图控制器。您需要在
tableView(\utableview:UITableView,cellForRowAt indexath:indexPath)中设置委托
方法:

此外,在初始化单元时,我们应使用可重用性,包括:

let cell = tableView.dequeueReusableCell(withIdentifier:"Cell") as! PostCellView
将您的方法更新为:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"Cell") as! PostCellView
    cell.delegate = self // Assign delegate
    return cell
}

您需要满足委托人的要求。只有这样,你才能接到电话。所以加上

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = PostCellView(style: UITableViewCellStyle.default , reuseIdentifier: "Cell")
    cell.delegate = self
    return cell
}

您忘记为单元格设置委托。cell.delegate=self在tableView(:,cellForRowAt:)中,您在ViewController中设置的
self
?请阅读,然后您的问题显示了演示问题的最低代码谢谢@Sh_Khan,我错过了这一部分。