2016-07-21 104 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個按鈕第二個按鈕會選擇像其他按鈕相同的行動。檢查我的答案。