Swift 4.2: Unable to add custom behaviour to a custom UITableViewCell using Protocol oriented delegate...












1















I've a custom UITableViewCell, in that I've two UILabels & one UIButton. I'm able to load data...and display it as per requirement.




  • Problem Statement-1: Now problem exist in my UIButton, which is in my UICustomTableViewCell. Due to this I'm unable to handle click event on that UIButton.


  • Problem Statement-2: On button Click I have to identify the index of that Button click and pass data to next ViewController using segue.





Now have a look on...what did I've tried for this...



Yes, first-of-all I have thought that Binding IBOutlet action in my CustomCell will resolve my problem...but actually it doesn't solved my problem.



After that I've accessed button using .tag and initialised index path.row to it.



But it won't helped me.



So now I'm using Protocol oriented concept using delegate to handle click event on my UIButton which is available in CustomCell.





What did I tried:



SwiftyTableViewCellDelegate:



protocol SwiftyTableViewCellDelegate : class {
func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell)
}




  • CustomTableViewCell with delegate:



    class LeadCustomTableViewCell: UITableViewCell {

    @IBOutlet weak var lblMeetingPersonName: UILabel!
    @IBOutlet weak var lblPolicyNo: UILabel!
    @IBOutlet weak var btnLeadAuditTrail: UIButton!


    weak var delegate: SwiftyTableViewCellDelegate?

    override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    }

    @IBAction func btnAuditTrailTapped(_ sender: UIButton) {
    delegate?.btnAuditTrailDidTapButton(self)
    }
    }







  • ViewController implementing delegate:



    class LeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwiftyTableViewCellDelegate {

    //IBOutlet Connections - for UITableView
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    //setting dataSource & delegates of UITableView with this ViewController
    self.tableView.dataSource = self
    self.tableView.delegate = self

    //Reloading tableview with updated data
    self.tableView.reloadData()

    //Removing extra empty cells from UITableView
    self.tableView.tableFooterView = UIView()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:LeadCustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! LeadCustomTableViewCell

    //Assigning respective array to its associated label
    cell.lblMeetingPersonName.text = (meetingPersonNameArray[indexPath.section] )
    cell.lblPolicyNo.text = (String(policyNoArray[indexPath.section]))

    cell.btnLeadAuditTrail.tag = indexPath.section
    cell.delegate = self

    return cell
    }

    //This is delegate function to handle buttonClick event

    func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell) {
    guard let tappedIndexPath = tableView.indexPath(for: sender) else { return }
    print("AuditTrailButtonClick", sender, tappedIndexPath)
    }


    Don't know why this is not working.












share|improve this question

























  • Does the print statement in the delegate method print anything?

    – hardik parmar
    Nov 27 '18 at 10:11











  • Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

    – Paulw11
    Nov 27 '18 at 10:14











  • @hardikparmar no it does not print any thing.

    – ASHISH
    Nov 27 '18 at 10:18











  • Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

    – hardik parmar
    Nov 27 '18 at 10:19











  • Okay Sure, I'll try it.

    – ASHISH
    Nov 27 '18 at 10:20
















1















I've a custom UITableViewCell, in that I've two UILabels & one UIButton. I'm able to load data...and display it as per requirement.




  • Problem Statement-1: Now problem exist in my UIButton, which is in my UICustomTableViewCell. Due to this I'm unable to handle click event on that UIButton.


  • Problem Statement-2: On button Click I have to identify the index of that Button click and pass data to next ViewController using segue.





Now have a look on...what did I've tried for this...



Yes, first-of-all I have thought that Binding IBOutlet action in my CustomCell will resolve my problem...but actually it doesn't solved my problem.



After that I've accessed button using .tag and initialised index path.row to it.



But it won't helped me.



So now I'm using Protocol oriented concept using delegate to handle click event on my UIButton which is available in CustomCell.





What did I tried:



SwiftyTableViewCellDelegate:



protocol SwiftyTableViewCellDelegate : class {
func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell)
}




  • CustomTableViewCell with delegate:



    class LeadCustomTableViewCell: UITableViewCell {

    @IBOutlet weak var lblMeetingPersonName: UILabel!
    @IBOutlet weak var lblPolicyNo: UILabel!
    @IBOutlet weak var btnLeadAuditTrail: UIButton!


    weak var delegate: SwiftyTableViewCellDelegate?

    override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    }

    @IBAction func btnAuditTrailTapped(_ sender: UIButton) {
    delegate?.btnAuditTrailDidTapButton(self)
    }
    }







  • ViewController implementing delegate:



    class LeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwiftyTableViewCellDelegate {

    //IBOutlet Connections - for UITableView
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    //setting dataSource & delegates of UITableView with this ViewController
    self.tableView.dataSource = self
    self.tableView.delegate = self

    //Reloading tableview with updated data
    self.tableView.reloadData()

    //Removing extra empty cells from UITableView
    self.tableView.tableFooterView = UIView()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:LeadCustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! LeadCustomTableViewCell

    //Assigning respective array to its associated label
    cell.lblMeetingPersonName.text = (meetingPersonNameArray[indexPath.section] )
    cell.lblPolicyNo.text = (String(policyNoArray[indexPath.section]))

    cell.btnLeadAuditTrail.tag = indexPath.section
    cell.delegate = self

    return cell
    }

    //This is delegate function to handle buttonClick event

    func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell) {
    guard let tappedIndexPath = tableView.indexPath(for: sender) else { return }
    print("AuditTrailButtonClick", sender, tappedIndexPath)
    }


    Don't know why this is not working.












share|improve this question

























  • Does the print statement in the delegate method print anything?

    – hardik parmar
    Nov 27 '18 at 10:11











  • Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

    – Paulw11
    Nov 27 '18 at 10:14











  • @hardikparmar no it does not print any thing.

    – ASHISH
    Nov 27 '18 at 10:18











  • Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

    – hardik parmar
    Nov 27 '18 at 10:19











  • Okay Sure, I'll try it.

    – ASHISH
    Nov 27 '18 at 10:20














1












1








1








I've a custom UITableViewCell, in that I've two UILabels & one UIButton. I'm able to load data...and display it as per requirement.




  • Problem Statement-1: Now problem exist in my UIButton, which is in my UICustomTableViewCell. Due to this I'm unable to handle click event on that UIButton.


  • Problem Statement-2: On button Click I have to identify the index of that Button click and pass data to next ViewController using segue.





Now have a look on...what did I've tried for this...



Yes, first-of-all I have thought that Binding IBOutlet action in my CustomCell will resolve my problem...but actually it doesn't solved my problem.



After that I've accessed button using .tag and initialised index path.row to it.



But it won't helped me.



So now I'm using Protocol oriented concept using delegate to handle click event on my UIButton which is available in CustomCell.





What did I tried:



SwiftyTableViewCellDelegate:



protocol SwiftyTableViewCellDelegate : class {
func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell)
}




  • CustomTableViewCell with delegate:



    class LeadCustomTableViewCell: UITableViewCell {

    @IBOutlet weak var lblMeetingPersonName: UILabel!
    @IBOutlet weak var lblPolicyNo: UILabel!
    @IBOutlet weak var btnLeadAuditTrail: UIButton!


    weak var delegate: SwiftyTableViewCellDelegate?

    override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    }

    @IBAction func btnAuditTrailTapped(_ sender: UIButton) {
    delegate?.btnAuditTrailDidTapButton(self)
    }
    }







  • ViewController implementing delegate:



    class LeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwiftyTableViewCellDelegate {

    //IBOutlet Connections - for UITableView
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    //setting dataSource & delegates of UITableView with this ViewController
    self.tableView.dataSource = self
    self.tableView.delegate = self

    //Reloading tableview with updated data
    self.tableView.reloadData()

    //Removing extra empty cells from UITableView
    self.tableView.tableFooterView = UIView()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:LeadCustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! LeadCustomTableViewCell

    //Assigning respective array to its associated label
    cell.lblMeetingPersonName.text = (meetingPersonNameArray[indexPath.section] )
    cell.lblPolicyNo.text = (String(policyNoArray[indexPath.section]))

    cell.btnLeadAuditTrail.tag = indexPath.section
    cell.delegate = self

    return cell
    }

    //This is delegate function to handle buttonClick event

    func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell) {
    guard let tappedIndexPath = tableView.indexPath(for: sender) else { return }
    print("AuditTrailButtonClick", sender, tappedIndexPath)
    }


    Don't know why this is not working.












share|improve this question
















I've a custom UITableViewCell, in that I've two UILabels & one UIButton. I'm able to load data...and display it as per requirement.




  • Problem Statement-1: Now problem exist in my UIButton, which is in my UICustomTableViewCell. Due to this I'm unable to handle click event on that UIButton.


  • Problem Statement-2: On button Click I have to identify the index of that Button click and pass data to next ViewController using segue.





Now have a look on...what did I've tried for this...



Yes, first-of-all I have thought that Binding IBOutlet action in my CustomCell will resolve my problem...but actually it doesn't solved my problem.



After that I've accessed button using .tag and initialised index path.row to it.



But it won't helped me.



So now I'm using Protocol oriented concept using delegate to handle click event on my UIButton which is available in CustomCell.





What did I tried:



SwiftyTableViewCellDelegate:



protocol SwiftyTableViewCellDelegate : class {
func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell)
}




  • CustomTableViewCell with delegate:



    class LeadCustomTableViewCell: UITableViewCell {

    @IBOutlet weak var lblMeetingPersonName: UILabel!
    @IBOutlet weak var lblPolicyNo: UILabel!
    @IBOutlet weak var btnLeadAuditTrail: UIButton!


    weak var delegate: SwiftyTableViewCellDelegate?

    override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    }

    @IBAction func btnAuditTrailTapped(_ sender: UIButton) {
    delegate?.btnAuditTrailDidTapButton(self)
    }
    }







  • ViewController implementing delegate:



    class LeadViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwiftyTableViewCellDelegate {

    //IBOutlet Connections - for UITableView
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    //setting dataSource & delegates of UITableView with this ViewController
    self.tableView.dataSource = self
    self.tableView.delegate = self

    //Reloading tableview with updated data
    self.tableView.reloadData()

    //Removing extra empty cells from UITableView
    self.tableView.tableFooterView = UIView()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:LeadCustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! LeadCustomTableViewCell

    //Assigning respective array to its associated label
    cell.lblMeetingPersonName.text = (meetingPersonNameArray[indexPath.section] )
    cell.lblPolicyNo.text = (String(policyNoArray[indexPath.section]))

    cell.btnLeadAuditTrail.tag = indexPath.section
    cell.delegate = self

    return cell
    }

    //This is delegate function to handle buttonClick event

    func btnAuditTrailDidTapButton(_ sender: LeadCustomTableViewCell) {
    guard let tappedIndexPath = tableView.indexPath(for: sender) else { return }
    print("AuditTrailButtonClick", sender, tappedIndexPath)
    }


    Don't know why this is not working.









ios swift4.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 8:57









Cœur

18.5k9110148




18.5k9110148










asked Nov 27 '18 at 9:57









ASHISHASHISH

65




65













  • Does the print statement in the delegate method print anything?

    – hardik parmar
    Nov 27 '18 at 10:11











  • Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

    – Paulw11
    Nov 27 '18 at 10:14











  • @hardikparmar no it does not print any thing.

    – ASHISH
    Nov 27 '18 at 10:18











  • Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

    – hardik parmar
    Nov 27 '18 at 10:19











  • Okay Sure, I'll try it.

    – ASHISH
    Nov 27 '18 at 10:20



















  • Does the print statement in the delegate method print anything?

    – hardik parmar
    Nov 27 '18 at 10:11











  • Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

    – Paulw11
    Nov 27 '18 at 10:14











  • @hardikparmar no it does not print any thing.

    – ASHISH
    Nov 27 '18 at 10:18











  • Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

    – hardik parmar
    Nov 27 '18 at 10:19











  • Okay Sure, I'll try it.

    – ASHISH
    Nov 27 '18 at 10:20

















Does the print statement in the delegate method print anything?

– hardik parmar
Nov 27 '18 at 10:11





Does the print statement in the delegate method print anything?

– hardik parmar
Nov 27 '18 at 10:11













Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

– Paulw11
Nov 27 '18 at 10:14





Set a breakpoint in btnAuditTrailTapped - Is that function being called? If not, you may not have connected the touchUpInside event to the function.

– Paulw11
Nov 27 '18 at 10:14













@hardikparmar no it does not print any thing.

– ASHISH
Nov 27 '18 at 10:18





@hardikparmar no it does not print any thing.

– ASHISH
Nov 27 '18 at 10:18













Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

– hardik parmar
Nov 27 '18 at 10:19





Set a breakpoint or put a print statement before guard let statement. and see if it comes there or not.

– hardik parmar
Nov 27 '18 at 10:19













Okay Sure, I'll try it.

– ASHISH
Nov 27 '18 at 10:20





Okay Sure, I'll try it.

– ASHISH
Nov 27 '18 at 10:20












1 Answer
1






active

oldest

votes


















0














Link the touch up inside event in cellForRow by adding the following code:



cell.btnLeadAuditTrail.addTarget(self, action:#selector(btnAuditTrailDidTapButton(_:)), for: .touchUpInside)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497044%2fswift-4-2-unable-to-add-custom-behaviour-to-a-custom-uitableviewcell-using-prot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Link the touch up inside event in cellForRow by adding the following code:



    cell.btnLeadAuditTrail.addTarget(self, action:#selector(btnAuditTrailDidTapButton(_:)), for: .touchUpInside)





    share|improve this answer




























      0














      Link the touch up inside event in cellForRow by adding the following code:



      cell.btnLeadAuditTrail.addTarget(self, action:#selector(btnAuditTrailDidTapButton(_:)), for: .touchUpInside)





      share|improve this answer


























        0












        0








        0







        Link the touch up inside event in cellForRow by adding the following code:



        cell.btnLeadAuditTrail.addTarget(self, action:#selector(btnAuditTrailDidTapButton(_:)), for: .touchUpInside)





        share|improve this answer













        Link the touch up inside event in cellForRow by adding the following code:



        cell.btnLeadAuditTrail.addTarget(self, action:#selector(btnAuditTrailDidTapButton(_:)), for: .touchUpInside)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 10:32









        Mahak MittalMahak Mittal

        101110




        101110
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497044%2fswift-4-2-unable-to-add-custom-behaviour-to-a-custom-uitableviewcell-using-prot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)