【问题标题】:click button action in sequentially order in UITableView在 UITableView 中按顺序单击按钮操作
【发布时间】:2023-03-06 19:11:01
【问题描述】:

在 UITableView 中创建了 4x4 指标按钮。 我想按顺序选择按钮。

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

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

按顺序的按钮操作顺序,对于任何随机行。 用户选择第三行,但第三行中的选择行从第一个按钮开始,然后是第二个按钮,然后是第三个按钮,最后是第四个按钮。

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

【问题讨论】:

  • 到底是什么问题?
  • @Lion 我想在任何随机 UITableView 行中依次选择按钮。 4x4 4 行 for UITableview 每行有 4 个按钮。单击按钮后,它将处于禁用模式。
  • the button's in sequencally in any random UITableView row 这条线是什么意思?

标签: ios objective-c uitableview uibutton


【解决方案1】:

设置适当的值,以便您可以识别按钮列以及行位置。 buttonIndex 只是保存 i 值,因此您只会获取列位置。您可以设置 button.tag=indexPath.row 并在 buttonAction 方法中使用它来了解行位置。

【讨论】:

    【解决方案2】:

    在您的 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
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        它用下面的代码解决了这个问题。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-13
          • 1970-01-01
          • 1970-01-01
          • 2013-01-02
          • 1970-01-01
          相关资源
          最近更新 更多