在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 回答
设置适当的值,以便您可以识别按钮列和行位置 . buttonIndex只保存i值,因此您只能获得列位置 . 您可以设置button.tag = indexPath.row并在buttonAction方法中使用它来知道行位置 .
在viewController中全局声明
NSInteger
tagValue .在上述方法的帮助下,您可以获得
indexpath.row
值,您可以从中确定tableview中每行的按钮 .在
CellForRowAtIndexPath
中设置UIButton标签,如下所示:和
它解决了以下代码的问题 .