Check Nerburikun’s solution in this thread: viewtopic.php?f=227&t=4064
Here is explanation of his solution. (Please don’t mind my grammar)
So now we have following structure:
1. We have a table - ItemsViewController(IVC). That table is constructed from cells using method: "tableView: cellForRowAtIndexPath: ".
2. We wanted our cell to be custom so we created a custom class of type UITableViewCell - "HomepwnerItemCell" (HIC). In that custom class we have a property UIImageView *thumbnailView that represents a thumbnail-button that show image of BNTItem. Since that image pops out in a new "popover" view - we also created a separate UIViewController for it - ImageViewController.
We had problem, because an HIC had to have access to images to fill the view of ImageViewController when the thumbnail button was clicked. But HIC is a "view" object, not a "controller". Therefore we wrote a method for the HIC so that it simply passes to HIC's controller, which is IVC, what method should be implemented.
Now, that was a good solution, but what if we have another cell that also want's to pass a method to its controller? We would have to implement similar code in that cell. And that will happen for every cell.
Instead of doing that, we create another class type of UITableViewCell that will be the “base” class. That “base” cell class will take the role of passing/forwarding methods from cells to controller, so that cell classes don’t need to do that every time.
Back to the code.
3. In the "tableView: cellForRowAtIndexPath: " method we create an instance of HIC class and call it “cell”.
We set properties of the “cell” such as: nameLabel, serialNumberLabel, valueLabel, etc., Every HIC objects has these properties. We list them in HIC.h file.
Because HIC is a subclass of our “base” class BNRCellBase, HIC objects also inherit properties from its parent class. In BNRCellBase we also has properties “controller” and “tableView”, therefore, HIC objects also has them. We set them too.
4.We created our HIC object and one of its abilities is when a thumbnail is pressed, it will show us an instance of ImageViewController (with an image of BNRItem inside). That ability to show images is possible because of method "showImage: " that we implement in the HIC.m file. When a thumbnail is pressed, the "showImage: " is sent to the instance of HIC. What’s happening inside that method? Inside we send following message:
- -that isn’t a standard method and we suppose to implement it somewhere in the HIC.m file. But we don’t have it there. When a compiler can’t find a method in the .m file - it goes to class’s parent class and looks inside his .m file. HIC’s parent class is our “base” class - BNRCellBase class. Its .m file implements that method:
- Inside of the implementation we take method that was passed as an argument ( (SEL)act ), which is in our case was "showImage: " method from HIC.m. We convert name of that method to a string so that we can modify it a little and add "atIndexPath: " to the end. Index path is necessary to specify the address of that cell in the table. We convert the string back to a method and send that method to the controller (IVC):
- In the controller’s .m class (IVC.m) we suppose to implement a method that was passed from the method “actionSender:(SEL)act fromObject:(id)obj”. It should append on "atIndexPath: " to be coherent with previous step (we already wrote its implementation)
Hope it was helpful to you.