Doj Yias Lem
Doj Yias Lem

Reputation: 137

Button actions in Custom cell not calling delegate functions

I am using this code in the file's owner of the view I am using as custom cells in my table view:

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

    }

}

and this in the view controller having the table 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.
    }


}

As expected on clicking the buttons, the delegate should have called postSettingsAction() , however this doesn't happen. What's possibly wrong here and how should I rectify this?

Upvotes: 2

Views: 60

Answers (2)

Arti
Arti

Reputation: 305

You need to satisfy the delegate. only then you will be call to receive the call. so add

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

Upvotes: 1

Ankit Jayaswal
Ankit Jayaswal

Reputation: 5679

You haven't assigned the delegate to your view controller. You need to set the delegate in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) method:

Also, we should use the reusability while initialising the cell, with:

let cell = tableView.dequeueReusableCell(withIdentifier:"Cell") as! PostCellView

Updated your method as:

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

Upvotes: 1

Related Questions