如何调用按钮操作取决于uiTableView中的标记?

时间:2015-08-03 07:08:04

标签: ios objective-c uitableview uibutton

我在单个单元格中有一个按钮,并为按钮设置了标记。

按钮uparrow创建:

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按钮操作方法

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

当我单击单独单元格中的按钮时,增量将继续显示以前的数据。

4 个答案:

答案 0 :(得分:1)

请使用以下代码。

spring.datasource.url=jdbc:mysql://localhost/database
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

并检查tagVal变量值。

答案 1 :(得分:0)

对于tableview中的标记

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

按钮操作

    NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
    NSUInteger row     = (sender.tag & 0xFFFF);
    (int)i;

答案 2 :(得分:0)

试试这个:

-(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;
}

答案 3 :(得分:0)

创建UITableViewCell的子类

在.h文件中

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

在.m文件中

// 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--;
    }
}