How to get a link to Table View Controller from a custom table View Cell class

I have a custom table view cell and you created your own class. How to get a link to a table view controller from a cell?

I have this custom cellview class. In this case, I have a button that is associated with a tap event. When I click, I get the event perfectly, but I'm looking to hold on to the table view controller so that I can show the action sheet on top of the table view.

@interface MTPurchasedCartItemCell : UITableViewCell - (IBAction)onShareTap:(id)sender; @end 
+6
source share
3 answers

The way I do this will use a block event handler. In your MTPurchasedCartItemCell class MTPurchasedCartItemCell add the property to the header file as follows:

 @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 cellForRowAtIndexPath: ::

 ... cell.tapHandler = ^(id sender) { // do something } ... 
+3
source

You might also consider adding an open @property for a button to a custom cell.

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

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

This is if you are creating a button in Interface Builder. If you create a button in the code, you must add this line:

 @property (nonatomic) UIButton *myButton; 

Then, when you initialize or configure the cell from the table view controller, you now have a link to myButton and you can add an action to it with the table view controller as the target.

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

+2
source

I did something very similar that gets a table link from a user 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); } 
-1
source

Source: https://habr.com/ru/post/952695/


All Articles