-1

i have a UITableview that has unique cells, each cell has it's own class and they have actions that i want to connect to my main UITableviewcontroller

I attach a protocol and open it in the tableviewcontroller but it doesn't get read

how could I initialise it or what am I doing wrong ?

here is my cell class :

import UIKit

class AddFaxHeadlineTableViewCell: UITableViewCell {

    var delegate: AddFaxHeadlineProtocol?

    @IBOutlet weak var addButton: UIButton!

    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 onAddFaxNumberPressed(_ sender: Any) {
        delegate?.faxButtonPressed()

    }

}


protocol AddFaxHeadlineProtocol{
    func faxButtonPressed()
}

and in my tableviewcontroller I extend the protocol:

class SummaryMainTableViewController: UITableViewController, AddFaxHeadlineProtocol, AddEmailHeadlineProtocol {

but the function itself never gets read:

func faxButtonPressed() {

        var indexToInsert = 0

        for forIndex in 0..<sectionsData.count {
            // render the tick mark each minute (60 times)
            if (sectionsData[forIndex] == "addFaxHeadline") {
                indexToInsert = forIndex + 1
            }

        }


        sectionsData.insert("addNewFax", at: indexToInsert)
        mainTableView.reloadData()
    }
Dimple
  • 788
  • 1
  • 11
  • 27
Chief Madog
  • 1,738
  • 4
  • 28
  • 55

3 Answers3

5

You need to call:

cell.delegate = self

In your cellForRowAtIndex method

This is the common mistake done in protocols and delegates to forget to call delegate.

Here are few examples you can check all have missing is calling delegate:-

Swift delegate beetween two VC without segue

Delegate seems to not be working, according to the console

How to present another view controller after dismiss from navigation controller in swift?

Wings
  • 2,398
  • 23
  • 46
1

Another way to go inside the vc without protocols

let cell = ///

cell.addButton.tag = indexPath.row

cell.addButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)

}

@objc func buttonTapped(_ sender:UIButton) {

    print(sender.tag)

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Check that you are doing this:

cell.delegate = self (It's required)

Then improve your line of code like below. Because you will not set delegate then by calling this delegate method directly will get crashed.

@IBAction func onAddFaxNumberPressed(_ sender: Any) {
    if let delegateObject = delegate {
        delegateObject.faxButtonPressed()
    }
}

Second, In this line, delegateObject.faxButtonPressed(), you will need to send some parameter to identify that will cell is clicked. So you can pass here button tag or you can pass cell also.

nitin.agam
  • 1,949
  • 1
  • 17
  • 24