2

I have a button in a single cell and set tag for the button.

button uparrow creation:

UIButton *btn_uparrow=[[UIButton alloc]initWithFrame:CGRectMake(500, 20, 50, 50)];
[btn_uparrow setTitle:@"up" forState:UIControlStateNormal];
btn_uparrow.backgroundColor =[UIColor blackColor];
[btn_uparrow addTarget:self action:@selector(btn_up_arrow:) forControlEvents:UIControlEventTouchUpInside];
[btn_uparrow setTag:indexPath.row];
[cell addSubview:btn_uparrow];

uparrow button action method

-(void)btn_up_arrow:(UIButton*)click
{
 i++;
 NSLog(@"increment %d",i);
  if(i>=5)
  {
    NSLog(@"button increment %d",i);
    i--;
  }
}

When I click the button in separate cell the increment will continue on the previous data.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
Kishore kumar
  • 143
  • 1
  • 14
  • possible duplicate of [Get button click inside UI table view cell](http://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell) – Ponf Aug 03 '15 at 07:24

4 Answers4

1

Please use following code.

NSInteger tagVal = (UIButton*)click.tag;

and check tagVal variable value.

Sandeep Agrawal
  • 425
  • 3
  • 10
0

For tag in tableview

[btn_uparrow setTag:((indexPath.section & 0xFFFF) << 16) |(indexPath.row & 0xFFFF);   

In button action

    NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
    NSUInteger row     = (sender.tag & 0xFFFF);
    (int)i;
priyadharshini
  • 158
  • 1
  • 3
  • 16
0

Try this:

-(void)btn_up_arrow:(UIButton*)click{
    // get value stored in tag of the button
    NSInteger tagVal = click.tag;
    tagVal++;
    NSLog(@"increment %d",tagVal);
    if(tagVal>=5)
    {

        NSLog(@"button increment %d",tagVal);
        tagVal--;
    }
    // save the value when you are done working with it
    click.tag = tagVal;
}
0

Create a subclass of UITableViewCell

In .h file

 @interface CustomTableViewCell : UITableViewCell
 {
     UIButton *button;
     int i;//globally declaring variable i
 }
 @end

In .m file

// initialize the button in 'init' or any initializing function that you use,
button = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    [button setTitle:@"ds" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(onButtonAction) forControlEvents:UIControlEventTouchUpInside];
 NSLog(@"button instance created");//MAKE SURE THIS IS PRINTED
    [self addSubview:button];



-(void)onButtonAction
{
    i++;
    NSLog(@"increment %d",i);
    if(i>=5)
    {

        NSLog(@"button increment %d",i);
        i--;
    }
}
Shebin Koshy
  • 1,182
  • 8
  • 22
  • i already done this but this is not working ,when i click the another cell button its continue with the previous value its not starting at initial. – Kishore kumar Aug 03 '15 at 12:38