2016-07-21 160 views
1

下一个按钮我在第5个按钮上点击第一个按钮,只有第二个按钮应该启用点击第二个按钮第三个按钮应启用。直到第4个按钮。启用点击第一个按钮

任何一个可以建议我要实施将不胜感激。

buttonArray = [[NSArray alloc] initWithObjects:@"4",@"3",@"4", nil]; 

// Cell for row index path added this code。

for (int buttonCount = 0; buttonCount< [[buttonArray objectAtIndex:indexPath.row] intValue]; buttonCount++) 
{ 
    int buttonSpace = 10 * buttonCount + 10; 
    cell.Button = [[CustomGoalButton alloc] initWithFrame:CGRectMake(buttonSpace + (buttonCount * 50),35, 50, 50)]; 
    cell.Button.layer.cornerRadius = 25; 
    [cell.Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.Button.buttonIndex = buttonCount; 
    cell.Button.tag = 100 * (indexPath.row + 1)+ buttonCount; 
    if (buttonCount == 0) { 
     cell.Button.userInteractionEnabled = YES; 
    } 
    else{ 
     cell.Button.userInteractionEnabled = NO; 
    } 

    [cell.ScrollView addSubview:cell.Button]; 
} 


// Button Action 
    -(void)buttonAction:(CustomButton *)sender{ 
     UITableViewCell *cell = (UITableViewCell *)[self. UITableView dequeueReusableCellWithIdentifier:@"TableViewCell"]; 
     NSIndexPath *indexPath = [self. UITableView indexPathForCell:cell]; 
     if (sender.selectedRowIndex == indexPath.row) { 
      sender.backgroundColor = [UIColor redColor]; 
      sender.userInteractionEnabled = NO; 
      sender.tag = sender.tag+1; 
     } 
    } 
+0

你能分享一下你写的代码吗? –

+0

你在tableview或scrollview中使用了概念 –

+0

@Anbu。Karthik我在UITableView中使用 – kiran

回答

1

1)创建所有按钮的出口集合。使最后4个按钮禁用。

2)创建oultet这样,所有的行动应该每个按钮应该调用同样的方法。

下面的代码将解决您的需求。

@IBAction func allButtonsTapped(sender: AnyObject) { 
    let index = numberButtons.indexOf(sender as! UIButton) 
    if numberButtons.count > index+1 { 
     let button = numberButtons[index!+1] 
     button.enabled = true 
    } 

} 

编辑:OBJC代码

-(void) allButtonsTapped:(id)sender { 
    int index = [numberButtons indexOfObject:((UIButton *)sender)] 
    if numberButtons.count > index+1 { 
     UIButton *button = numberButtons.objectAtIndex(index+1) 
     button.enabled = true 
    } 
} 
+0

问题是关于Objective-c和快速亲爱的。 –

+0

@ Dev.RK好吧,看起来你是对的。 我会将代码更改为ObjC。 但你得到了代码背后的逻辑。 –

+0

@RiosK改变了objc的答案。让我知道这是否适合你? –

0

标记您的按钮,这样

button1.tag = 1 
button2.tag = 2 
button3.tag = 3 
button4.tag = 4 

所有四个按钮添加点击事件为 “buttonClicked”。

我假设你有四个按钮的表格视图单元格的属性。因此,当点击任何按钮时,请添加以下代码。

-(void) buttonClicked:(UIButton*)sender 
{ 
CGPoint center= sender.center; 
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tabelView]; 
NSIndexPath *indexPath = [self.tabelView indexPathForRowAtPoint:rootViewPoint]; 

UITableViewCell *cell = [_tabelView cellForRowAtIndexPath:indexPath]; 

switch (sender.tag) { 
    case 1: 
    { 
     [cell.btn1 setEnabled:NO]; 
     [cell.btn2 setEnabled:YES]; 
     [cell.btn3 setEnabled:NO]; 
     [cell.btn4 setEnabled:NO]; 
    } 
     break; 
    case 2: 
    { 
     [cell.btn1 setEnabled:NO]; 
     [cell.btn2 setEnabled:NO]; 
     [cell.btn3 setEnabled:YES]; 
     [cell.btn4 setEnabled:NO]; 
    } 
     break; 
    case 3: 
    { 
     [cell.btn1 setEnabled:NO]; 
     [cell.btn2 setEnabled:NO]; 
     [cell.btn3 setEnabled:NO]; 
     [cell.btn4 setEnabled:YES]; 
    } 
     break; 
    default: 
     break; 
    } 
} 

根据您的要求更改以上代码。希望它能帮助你。

+0

@女士不是我期望的,但感谢您的反馈。 – kiran

+0

@kiran然后你想要什么? –

+0

@kiran根据您的要求“按行按钮顺序,点击第一个按钮第二个按钮应该启用,点击第二个按钮第三个按钮启用,所有点击的按钮将被禁用模式”。上面的代码将工作。 –

2

我想你想要这种功能。我有示例代码,以便你会得到一个知道如何做到这 -

enter image description here

下面是示例代码 -

buttonArray = [[NSArray alloc] initWithObjects:@"4",@"3",@"4", nil]; 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return buttonArray.count; 

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    for (int buttonCount = 0; buttonCount< [[buttonArray objectAtIndex:indexPath.row] intValue]; buttonCount++) 
    { 
     int buttonSpace = 10 * buttonCount + 10; 
     UIButton* Button = [[UIButton alloc] initWithFrame:CGRectMake(buttonSpace + (buttonCount * 50),35, 50, 50)]; 
     Button.backgroundColor=[UIColor redColor]; 
     Button.layer.cornerRadius = 25; 
     [Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
     Button.tag = 100 * (indexPath.row + 1)+ buttonCount; 
     if (buttonCount == 0) { 
      Button.userInteractionEnabled = YES; 
      Button.backgroundColor=[UIColor greenColor]; 
     } 
     else{ 
      Button.userInteractionEnabled = NO; 
      Button.backgroundColor=[UIColor redColor]; 
     } 

     [cell addSubview:Button]; 
    } 

    return cell; 

} 
// Button Action 
-(void)buttonAction:(UIButton *)sender{ 

    UITableViewCell *cell = (UITableViewCell *)[sender superview]; 

    NSIndexPath *indexPath=[self.tableView indexPathForCell:cell]; 

    if ([cell viewWithTag:sender.tag+1]==nil) { 

     UIButton *btn=(UIButton*)[cell viewWithTag:100 * (indexPath.row + 1)+ 0]; 

     btn.backgroundColor=[UIColor greenColor]; 

     btn.userInteractionEnabled=YES; 

     sender.userInteractionEnabled=false; 

     sender.backgroundColor=[UIColor redColor]; 

    }else{ 

    UIButton *btn=(UIButton*)[cell viewWithTag:sender.tag+1]; 

    btn.backgroundColor=[UIColor greenColor]; 

    btn.userInteractionEnabled=YES; 

    sender.userInteractionEnabled=false; 

    sender.backgroundColor=[UIColor redColor]; 

    } 

} 

注意:如果你不想再循环删除,如果condtion即if ([cell viewWithTag:sender.tag+1]==nil)buttonAction,只能使用其他部分。希望这会帮助你解决你的问题。

0

你的问题的确切答案。我尝试过你的样品。它完美的工作。你可以使用相同的代码并获得解决方案。

ViewController.m

#import "ViewController.h" 
#import "CustomCell.h" 
@interface ViewController() 
{ 
    NSMutableArray *arrayTableData; 
} 
@end 
@implementation ViewController 
@synthesize tableviewCustomCellButton 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    arrayTableData = [[NSMutableArray alloc]initWithObjects:@"Asia",@"Europe",@"Atlantic",@"Africa",@"Antartica",nil]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

的#pragma马克 - 的UITableViewDelegate方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrayTableData.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if(cell==nil) 
    { 
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = nibs[0]; 
    } 
    cell.labelData.text = [arrayTableData objectAtIndex:indexPath.row]; 

    [cell.btnClick addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.btnClick setTitle:[NSString stringWithFormat:@"%ld",(long)indexPath.row] forState:UIControlStateNormal]; 
    [cell.btnClick setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 

    cell.btnClick.tag = indexPath.row; 

    return cell; 
} 

-(void)buttonAction:(UIButton *)sender 
{ 
    UIButton *btnNext = sender; 
    int buttonTag = (int)sender.tag; 
    NSLog(@"The buttonTag is - %d",buttonTag); 
    buttonTag = buttonTag+1; 
    NSIndexPath *indexPath; 
    CustomCell *cellBut; 
    if(buttonTag < arrayTableData.count) 
    { 
    for(int i=0;i<arrayTableData.count;i++) 
    { 
      if(buttonTag == i) 
      { 
      indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:0]; 
      cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath]; 
      btnNext = (UIButton*)[cellBut viewWithTag:buttonTag]; 
      btnNext.selected = YES; 
      } 
      else 
      { 
      indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
      cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath]; 
      btnNext = (UIButton*)[cellBut viewWithTag:i]; 
      btnNext.selected = NO; 
      } 
     } 
    } 
    else 
    { 
    for(int i=0;i<arrayTableData.count;i++) 
    { 
     if(buttonTag == 5) 
     { 
      buttonTag = buttonTag - (int)[arrayTableData count]; 
      indexPath = [NSIndexPath indexPathForRow:buttonTag inSection:0]; 
      cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath]; 
      btnNext = (UIButton*)[cellBut viewWithTag:buttonTag]; 
      btnNext.selected = YES; 
     } 
     else 
     { 
      indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
      cellBut = (CustomCell *)[tableviewCustomCellButton cellForRowAtIndexPath:indexPath]; 
      btnNext = (UIButton*)[cellBut viewWithTag:i]; 
      btnNext.selected = NO; 
     } 
    } 
    } 
} 

在上面的代码中我用标签和Button.If自定义单元格您单击1个按钮第二个按钮会选择像其他按钮相同的行动。检查我的答案。