0

I have a custom class "TableViewCell" and "ViewController". I implemented func TapGestureRecognizer in IBOutlet Label at "TableViewCell". Problem is in the func, there's a mainCounter variable which counts how many times the label is tapped, and I'd like to get this value in "ViewController". I tried making TableViewCell's instance in ViewController and access it, but it's just a new instance so I could only get initial value 0. Can anyone give me even simple hint? here's two classes

'''

class TableViewCell: UITableViewCell {

    @IBOutlet weak var showTimeLabel: UILabel!

    var index = 0
    var timeArr:[TimeStringLabel] = [TimeStringLabel]()

//******************************************
    public var mainCounter:Int = 0 
//******************************************

    @objc func labelTapped(_ sender: UITapGestureRecognizer) {

        if index >= timeArr.count {
        }

        else {
            self.mainCounter = timeArr[index].timeCounter        
            index += 1
            print(mainCounter) //In here, I can get values I want
        }
      }

    func setupLabelTap() {

           let labelTap = UITapGestureRecognizer(target: self, action: #selector(self.labelTapped(_:)))
           self.showTimeLabel.isUserInteractionEnabled = true
           self.showTimeLabel.addGestureRecognizer(labelTap)

       }
'''

'''

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var tableView: UITableView!

    var mainCounter:Int = 0

    var tableViewCell = TableViewCell()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        self.tableView.delegate = self
        self.tableView.dataSource = self

    }

    @IBAction func resetButton(_ sender: Any) {

       print(tableViewCell.mainCounter) //In here, I can get only 0

    }

'''

Jiho
  • 41
  • 9
  • Do you want all cells to increment `mainCounter`? – Alexander Apr 27 '20 at 15:17
  • I want only selected Cell to be increment. I have 2 functions for selecting cell in the viewcontroller – Jiho Apr 27 '20 at 15:20
  • 1
    Usually, for a relationship like this, you would make a stored property on the cells like `weak var delegate: MyTableViewDelegateProtocol`. `MyTableViewDelegateProtocol?` is a protocol that contains everything a cell might want to inform its delegate about. In this case `func labelWasTapped()`, or something to that effect. You would extend `ViewController` to implement `MyTableViewDelegateProtocol`, and set the `delegate` property of each cell to point to the view controller. Then, when the label gets tapped, you call `self.delegate?.labelWasTapped()`. – Alexander Apr 27 '20 at 15:58
  • 1
    From there, the ViewController can decide what to do in response to a label tap, in this case, increment `mainCounter`, but only if the tap came from a selected sell (which you can determine by changing `func labelWasTapped()` to `func labelWasTapped(sender: TableViewCell)`) – Alexander Apr 27 '20 at 15:59
  • Thank you so much! it really helps me a lot! – Jiho Apr 27 '20 at 16:08
  • 1
    See the techniques here https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 Apr 27 '20 at 21:27

0 Answers0