Omar Alhadidy
Omar Alhadidy

Reputation: 109

Update the value of table cell variable afterward

I created a tableview with a custom cell.

Inside my UITableViewCell file I have the following code :

var myTipView:EasyTipView?

@IBAction func infoBtn_pressed(_ sender: UIButton) {
    //print(diseases)
    if self.myTipView == nil
    {
        self.myTipView = EasyTipView(text: diseases, preferences: EasyTipView.globalPreferences)
        self.myTipView!.show(forView: sender)

    }
    else
    {
        self.myTipView!.dismiss()
        self.myTipView = nil
    }
}

and inside my UIViewController I have a tableview with the code :

var myTipView:EasyTipView?


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = myTable.dequeueReusableCell(withIdentifier: "diffCell") as! diffCell

 cell.myTipView = self.myTipView

}

the when I tried to use the value of self.myTipView I found it nil and this is obvious because it have no value until the infoBtn_pressed is activated and in this case cell.myTipView is always nil when the table first created

how can i get self.myTipView to have the value after the button is pressed to use it inside UIViewController

Upvotes: 0

Views: 162

Answers (3)

Omar Alhadidy
Omar Alhadidy

Reputation: 109

Finally i used delegates to notify the table when a button is clicked in the table cell and worked great (^^)

Upvotes: 0

Marcus
Marcus

Reputation: 2321

You could try adding an observer to your myTipView parameter:

var myTipView : EasyTipView? {
    didSet {
        // Test that myTipView is non-nil
        if let _ = myTipView {
             // implement a function here that  updates the cells in the table view...
        }
     }
}

I'm assuming you can handle updating the table cells (incidentally, your implementation seems to suggest that every cell has the same EasyTipView property: is that your intention?). Hope that helps.

Upvotes: 1

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

Once you set the myTipView value, You need to refresh the table by doing this :-

tableView.reloadData()

Upvotes: 2

Related Questions