7

I have custom table view cell and have made a custom class. How can I get reference to the table view controller from the cell?

I have this custom tableview cell class. In this I have a button which is connected with a tap event. On tap I get the event fine but I am looking to get a hold on the table view controller so I can show the action sheet on top of the table view.

@interface MTPurchasedCartItemCell : UITableViewCell

- (IBAction)onShareTap:(id)sender;

@end
Encore PTL
  • 8,084
  • 10
  • 43
  • 78
  • You will have to give more data, even code. What are you trying to do? what have you done? What do you mean by pointer? For what do you need a pointer? – CaptJak Aug 28 '13 at 22:07
  • I have added some code and updated my question. Thanks. Basically I want to get access to Table View Controller so I can show action sheet because I have a button inside the table view cell. – Encore PTL Aug 28 '13 at 22:11
  • The button's target should be set to the controller, which you set up by passing self as the target argument in addTarget:action:forControlEvents: – rdelmar Aug 28 '13 at 22:13
  • OK I thought if there is a way to get access to table view controller through the cell then I could just invoke that action from the table cell – Encore PTL Aug 28 '13 at 22:14
  • It would be a lot easier to present the action sheet from the button frame. – progrmr Aug 29 '13 at 01:13
  • wy cant u set delegate to tableview when tapped on cell it calls a delegate method to ur tableview controller calss – Shankar BS Aug 29 '13 at 04:59

3 Answers3

4

The way I would do this would be to use a block event handler. In your MTPurchasedCartItemCell class add a property in the header file like so:

@property (nonatomic, copy) void (^tapHandler)(id sender);

And in the implementation file you can do this:

- (IBAction)onShareTap:(id)sender {
    if (self.tapHandler) {
        tapHandler(sender);
    }
}

And finally in your controller class, do something like this in your cellForRowAtIndexPath::

...
cell.tapHandler = ^(id sender) {
    // do something
}
...
edc1591
  • 10,146
  • 6
  • 40
  • 63
  • 1
    As the Buddha warns Siddhartha, be careful about being too clever. [Using target-action](http://stackoverflow.com/a/19608500/242933) is a simpler way to do the same thing. – ma11hew28 Oct 26 '13 at 15:45
2

You might also consider adding a public @property for the button to the custom cell.

To do that, add the following line to the header file of your custom cell class:

@property (weak, nonatomic) IBOutlet UIButton *myButton;

That's if you're creating the button in Interface Builder. If you're creating the button in code, you'd add this line instead:

@property (nonatomic) UIButton *myButton;

Then, when initializing or configuring the cell from your table view controller, you now have a reference to myButton and can add an action to it with your table view controller as the target.

Then, in the action method, you can get the cell from the button. The answer to Objective-C: How to generate one unique NSInteger from two NSIntegers? explains how.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
-1

I've done something very similar that gets the table reference from a custom cell using a button event.

Code:

-(IBAction)onShareTap:(UIButton *)sender { // i've used a button as input.. 

UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)senderButton.superview.superview.superview;
UITableView* table = (UITableView *)[buttonCell superview];

NSLog(@"Table: %@", table);
}
Shams Ahmed
  • 4,498
  • 4
  • 21
  • 27
  • It's dangerous to use `superview` to get a reference to the table view from one of its cells because the details of the view hierarchy is private and could change in a different version of iOS, which would break your code. Also, @EncorePTL is asking about how to get the table view controller, not the table view. – ma11hew28 Oct 26 '13 at 15:13