2016-07-21 4 views
0

UITableViewで4x4メトリックのボタンを作成しました。 ボタンを順番に選択したいと思います。UITableViewで順番にボタンのアクションをクリックします。

ビューコントローラにおける行インデックス

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番目のボタンまでです。

各行のUITableview 4ボタンの4x4 - > 4行。ボタンをクリックすると無効モードになります

+0

問題は何ですか? – Lion

+0

@LionランダムなUITableView行でボタンを順番に選択したいのですが。各行に4×4 4行の使用可能なビュー4ボタン。ボタンをクリックすると、無効モードになります。 – kiran

+0

'ボタンが順番に任意のUITableView行にあります'この行はどういう意味ですか? – Lion

答えて

0

ボタンの列と行の位置を認識できるように適切な値を設定します。 buttonIndexはiの値を保存しているだけなので、列の位置のみを取得します。button.tag = indexPath.rowを設定し、buttonActionメソッドで使用して行の位置を知ることができます。

0

NSInteger tagValueをviewController内にグローバルに宣言します。上記の方法の助けを借りて

-(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値を得ることができます。

[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 
} 
このよう CellForRowAtIndexPath
0

設定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; 
    } 
関連する問題