Reputation: 143
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.
Upvotes: 2
Views: 283
Reputation: 1192
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--;
}
}
Upvotes: 0
Reputation: 4213
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;
}
Upvotes: 0
Reputation: 158
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;
Upvotes: 0
Reputation: 425
Please use following code.
NSInteger tagVal = (UIButton*)click.tag;
and check tagVal variable value.
Upvotes: 1