首页 文章

在UITableView中按顺序单击按钮操作

提问于
浏览
0

在UITableView中创建了4x4 metrics按钮 . 我想按顺序选择按钮 .

//按钮在单元格中为行索引创建

for (int i = 0; i< [buttonArray count]; i++)
{
    int buttonSpace = 10 * i + 10;
    NSLog(@"ButtonPosition:%d",buttonSpace + (i * 50));
    cell.Button = [[CustomButton alloc] initWithFrame:CGRectMake(buttonSpace + (i * 50),35, 50, 50)];
    cell.Button.layer.cornerRadius = 25;
    [cell.Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.Button.buttonIndex = i;

    [cell.ScrollView addSubview:cell.Button];
    cell.ScrollView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.3];
}
    rowIndexValue = indexPath.row;

//视图控制器中的按钮操作 .

-(void)buttonAction:(CustomButton *)sender
{
    if (rowIndexValue) {

        int counter = (int)[sender buttonIndex];
            if (counter == incrementValue)
            {
                sender.backgroundColor = [UIColor redColor];
                counter += 1;
                incrementValue = counter;
            }
            else{
                sender.selected = NO;
            }
        }
}

按顺序排列按钮动作的顺序,对于任何随机行 . 用户选择第3行,但第3行中的选择行从第1个按钮开始,然后是第2个按钮,第3个按钮则从第4个按

4x4 - > 4行,每行UITableview 4按钮 . 单击按钮后,它将处于禁用模式

4 回答

  • 0

    设置适当的值,以便您可以识别按钮列和行位置 . buttonIndex只保存i值,因此您只能获得列位置 . 您可以设置button.tag = indexPath.row并在buttonAction方法中使用它来知道行位置 .

  • 0

    在viewController中全局声明 NSInteger tagValue .

    -(NSUInteger)getRowByBtn:(UIButton *)btn :(UITableView *)tblView
    {
        CGPoint center= btn.center;
        CGPoint rootViewPoint = [btn.superview convertPoint:center toView:tblView];
        NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:rootViewPoint];
        NSLog(@"indexPath = %ld", (long)indexPath.row);
        return indexPath.row;
    }
    

    在上述方法的帮助下,您可以获得 indexpath.row 值,您可以从中确定tableview中每行的按钮 .

    [cell.Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    -(void)buttonAction:(UIButton*)sender
    {     
        UIButton *btn=(UIButton *)sender;
        tagValue = [self getRowByBtn:btn :yourTableview];
        [[self.view viewWithTag:tagValue]setUserInteractionEnabled:YES];
        //or any other action
    }
    
  • 0

    CellForRowAtIndexPath 中设置UIButton标签,如下所示:

    [cell.btn_name addTarget:self action:@selector(onclick_button:) forControlEvents:UIControlEventTouchUpInside];
     self.btn_name.tag=indexpath.row;
    

    -(IBAction)onclick_button:(id)sender
    {
        int btn_index= ((UIView*)sender).tag;
        NSLog(@"Btn index:%d",btn_index);
    }
    
  • 0

    它解决了以下代码的问题 .

    -(void)buttonAction:(UIButton*)sender{
        CGPoint center = sender.center;
            CGPoint rootViewPoint = [sender.superview convertPoint:centre toView:self.tableView];
            NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:rootViewPoint];
            sender.userInteractionEnabled = NO;
            sender.backgroundColor = [UIColor redColor];
            CustomButton *enableNextButton = (CustomButton *) [[self.goalsTableView cellForRowAtIndexPath:indexPath] viewWithTag:sender.tag+1];
            enableNextButton.userInteractionEnabled = YES;
        }
    

相关问题