1

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?

mnort9
  • 1,810
  • 3
  • 30
  • 54

2 Answers2

0

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.

Community
  • 1
  • 1
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Can I pass an object with Notifications? If not, I'll attempt delegation. – mnort9 Jun 14 '12 at 20:31
  • 1
    Yes. The method `-postNotificationName:object:userInfo:` takes an NSDictionary of arguments in the usersInfo argument. Do _not_ pass objects through the object argument. – CodaFi Jun 14 '12 at 20:37
0

Having a reference of the tableController inside the cell is indeed Bad practice

You could fix this by implementing a special @protocol for your UITableViewClass And add a delegate method to it, and then implment the method inside UITableViewController, and since your UITableViewClass delegate is your UITableViewController, then you would call it like in your UITableViewClass.m

[delegate someMethod:data];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Bad? It's encouraged... Delegation, notifications, super... Heck, even the concept of a message itself suggests bi-directional communication. – CodaFi Jun 14 '12 at 19:21
  • the delegate calls methods in the delegate object, and notifications is to enforce objects decoupling, so i guess you even didn't understand what i wanted to say, or that i didn't explain it very good, either ways the -1 was not deserved, as if you read the answer carefully you would have noticed that what i do is implementing a new delegate :), i would be glad if you read it again, thanks – Omar Abdelhafith Jun 14 '12 at 19:40
  • @CodaFi ok, but do you still think that my answer is not the correct way to do it, because from what am reading your suggestion was to go with delegate too – Omar Abdelhafith Jun 14 '12 at 19:42
  • I'm withholding my vote on the basis that it's completely false to say `Its bad to have this sort of Bidiractionl communication` – CodaFi Jun 14 '12 at 19:43
  • hehe, as i said, i think both our answer are exact, what i meant is bad to hook both the classes together, since i assumed that what he was thinking is to add both the references to the other class in the other class, class a will reference class be and class b will reference a, but now that you say it i really should update my answer :) – Omar Abdelhafith Jun 14 '12 at 19:46