Search code examples
objective-ciosuitableviewencapsulation

Call method in UITableViewController from custom UITableViewCell


I need to call a method and pass an object from my custom UITableViewClass implementation to my UITableViewController class. I realize creating an instance of the tableViewController in the custom tableViewCell and calling tableViewController's method is a bad practice.

What is the proper way of doing this?


Solution

  • Two magical concepts in Objective-C are Delegation and Notifications.

    Delegation allows you to have your controller hook into a weak object referenced in the cell, which avoids a retain cycle, while still allowing you to send messages to it.

    Notifications allow your Cell to broadcast a general notification to any classes that are active and listening for it.

    Pick one, and whichever is easiest, stick with it. The two are basically equal in this situation.