0

I use UITableview, using IB I defined a UIButton in a custom cell and some labels, The custom cell subclass already have definitions of IBAction of the button and necessary IBOutlets on it, but I want to handle the button click events in the tableview controller it self but not in the custom cell subclass.

How can I do this? also I need to get which row's button is exactly clicked so I will show the content relavant to it.

Spring
  • 11,333
  • 29
  • 116
  • 185

3 Answers3

2

I solved problem by addding this into my controller;

[cell.expButton setTag:indexPath.row];
UIButton *button = (UIButton *)[cell expButton];
[button addTarget:self action:@selector(buttonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
Spring
  • 11,333
  • 29
  • 116
  • 185
0

For the need of clickable event(touch effect on the subView), I usually do this :

-(void)viewDidLoad{
    self.tableView.delaysContentTouches = NO;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // This will do the touch down effect on the subView of your UIButton
  for (id obj in cell.subviews)
    {
        if ([NSStringFromClass([obj class]) isEqualToString:@"UITableViewCellScrollView"])
        {
            UIScrollView *scroll = (UIScrollView *) obj;
            scroll.delaysContentTouches = NO;
            break;
        }
    }

   // I need to get which row's button is exactly clicked so I will show the content relavant to it.- here it is 
   UIButton *btn = (UIButton*)[cell viewWithTag:200];
   // btn = cell.myButton; If you are connected with the IBOutlet
   [btn setTag:200 + indexPath.row];
   [self.btnPlay addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)btnAction: (id)sender{
    UIButton *btn = (UIButton *)sender;
       NSLog(@"Selected subView of the  Row is  %d th tag", btn.tag-200);

   // You can get the UITableViewCell using the tag property
    selectedIP = [NSIndexPath indexPathForRow:btn.tag - 200 inSection:1;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIP];

  // Do the relevant code 
}
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0

the way how you want to do this is right (how MVC have to coded clean) - thats beautiful!

To make it more efficent you have to use delegation - thats the best way how to do this! Here a way how you can do this (e.g. in swift). https://stackoverflow.com/a/29920564/1415713

Community
  • 1
  • 1
kurtanamo
  • 1,808
  • 22
  • 27