Anuradh S Anuradh S - 6 months ago 50
iOS Question

how to fire a button action method in didselectrowatindexpath in iOS, objective c

I have a tableview with custom tableview cell. in the tableview cell there are two labels and one button.what I want it to fire the button action for user selected row to hide a label in the same row.

this is my controller for table view


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tablev;



#import "ViewController.h"
#import "TestTableViewCell.h"

@interface ViewController ()


@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return 2;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
cell.selectionStyle = UITableViewCellFocusStyleCustom;

return cell;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSInteger sec = indexPath.section;
NSInteger rw = indexPath.row;

TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
cell.numberlabel.hidden = YES;
NSLog(@"selected section :%li ---> selected row :%li",(long)sec, (long)rw);

//in here I want fire the button acction in the cell for each row when cell tap.(not when the button click in the cell).


#import <UIKit/UIKit.h>

@interface TestTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *staticlabel;
@property (weak, nonatomic) IBOutlet UILabel *numberlabel;

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


//I have tried to implement button click method here.It worked.but at that point it didn't recognised which cell is taped.

**NOTE : I have tried to implement button click method here.I worked.but at that point it didn't recognised which cell is taped. **


You can implement by two way one you can add Button Action in to your cellForRowAtIndexPath and setting tag of Button like following code:


[hidebutton addTarget:self

Its Action Method is

  NSIndexPath *hideIndexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
  TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:hideIndexpath];


Another way is you can achieve this same from DidSelect method with following code:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
            TestTableViewCell *cell = (TestTableViewCell *)[self.tablev cellForRowAtIndexPath:indexPath];
//use your cell object for hide anyting