2016-12-15 7 views
0

誰かが以下の要件を達成するのを助けることができれば素晴らしいことでしょう。 tableviewcellには、動的に追加されたuibuttonsを持つ水平スクロールビューがあります。ユーザーは1つの行から複数​​のボタンを選択できますが、異なる行からボタンを選択することはできません。たとえば、私はすでにボタンを選択しています(私は1つ以上のボタンを選択することができるはずです)row1で、私は行2のボタンをタップすると、row1の選択されたボタンは選択解除されなければならないと私はrow2でタップしたボタンを選択する必要があります。UITableViewCell UIButtonの選択

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

    cell.timeslotScrollView.contentSize = CGSizeMake(500, 0); 
    cell.timeslotScrollView.scrollEnabled = YES; 
    cell.timeslotScrollView.showsHorizontalScrollIndicator = NO; 

    UIButton *button = [[UIButton alloc]init]; 
    button.frame = CGRectMake(0, 0, 68, 35); 
    [button setTitle:@"abc" forState:UIControlStateNormal]; 

    button.layer.borderWidth = 2; 
    button.layer.borderColor = [UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0].CGColor; 
    button.layer.cornerRadius = 3; 
    button.userInteractionEnabled = YES; 
    [button setTitleColor:[UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0] forState:UIControlStateNormal]; 
    [button setTag:indexPath.row]; 
    [button addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside]; 

    UIButton *button1 = [[UIButton alloc]init]; 
    button1.frame = CGRectMake(button.frame.origin.x+68+buttonSpace, 0, 68, 35); 
    [button1 setTitle:@"5 pm" forState:UIControlStateNormal]; 
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 
    button1.layer.borderWidth = 2; 
    button1.layer.borderColor = [UIColor grayColor].CGColor; 
    button1.layer.cornerRadius = 3; 
    button1.userInteractionEnabled = YES; 
    [button1 setTag:indexPath.row]; 


    [button1 addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.timeslotScrollView addSubview:button]; 
    [cell.timeslotScrollView addSubview:button1]; 

    } 

    return cell; 
} 

-(void)didTap:(id)sender 
{ 
    UIButton *pressedButton = (UIButton *)sender; 
    if (pressedButton.tag != selectedButton) { 
    [sender setBackgroundColor:[UIColor greenColor]]; 

    selectedButton = pressedButton.tag; 


    } 
    else{ 
    [sender setBackgroundColor:[UIColor clearColor]]; 

    } 
} 
+0

コードはありますか?もしそうなら、投稿してください。そうでなければ、あなたの問題を解決しようとし、それでもそれを理解できなければ戻ってください。ありがとう –

+1

@CalebKleveter私は試したコードと一緒に私の質問を更新しました – user3310076

+0

あなたの質問は何ですか? – Koen

答えて

0

私は別のアプローチを試みます。私はUITableViewCellをサブクラス化して、セル実装ファイルの中にボタンを追加します。次に、このセルに、ボタンの選択/選択解除や実行する必要のあるその他のアクションをトリガーするメソッドを追加します。

この時点で、cellForRowAtIndexPathデリゲートでは、セル内のメソッドを呼び出して選択解除を実行することのみが懸念されます。

+0

uitableviewcellをサブクラス化しても、ボタンアクションメソッドを別途記述する必要があります。このメソッドでは、すべての選択/選択解除が行われると思います。したがって、サブクラス化するかどうかに大きな違いはありません。 – user3310076

0

私はこのようなものだろう:

- (void) tableView: (UITableView *) tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath { 
    [[tableView cellForRowAtIndexPath: indexPath] deselectAllButtons]; 
} 

を今毎回行が選択されていない取得、そのすべてのボタンは関係なく、他の行のいずれかに何が起こるか、選択解除されません。あなた自身がまだdeselectAllButtonsメソッドを作成する必要があります。 timeslotScrollViewのすべてのサブビューを繰り返します。

関連する問題