2016-04-26 26 views
1

ボタンアクション内でtouchupを使用して、テーブルビューのセルでラベルの背景色を変更する必要があります。私はテーブルビューでカスタムセルを作成し、セルに(NSMutableArrayを使用して)20行を追加してから、UIButtonを作成し、ボタンアクションをプログラムで実装しました(TouchUPInsideアクション)。テーブルセルのボタンラベルの背景色を変更するアクション

私はボタンをクリックするとラベルの背景色がその特定のインデックスラベル(特定の行)の緑色に変わりますが、私のコードではすべての行にアクションが反映されています。すべての行で変更されました。

//Array Declaration 
    kioskStatus = [NSMutableArray arrayWithObjects:@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open", nil]; 

//Table Cell Delegates 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *simpleTableIdentifier = @"Cell"; 

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; // Custom cell identifier for the string 
if (cell == nil) // Check the cell values are nill or not 
{ 
    cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; // Initialize the customTableviewCell identifer to the cell. 
} 


cell.kioskStat.text = [kioskStatus objectAtIndex:indexPath.row]; 


cell.detailsButton.tag=indexPath.row; 

**if(cell.detailsButton.selected==YES) 
{ 
    cell.kioskStat.textColor=[UIColor greenColor]; //if button is selected the label color to change green 
} 
else 
{ 
    cell.kioskStat.textColor=[UIColor blackColor]; //else the label color to change black 
}** 

//Button Action 
[cell.detailsButton addTarget:self action:@selector(detailsButtonAction:) forControlEvents: UIControlEventTouchUpInside]; 

return cell; // returns the cell values to the table view. 
} 


-(void)detailsButtonAction:(UIButton*)sender 

    { 
    [kioskStatus objectAtIndex:sender.tag] 

    NSLog(@"button tapped Index %lu",sender.tag); 

return [self.tableView reloadData]; //Reload the table view. 
} 

これを私のコード:

は、ここに私のコードです。私は、ボタンのアクションはいくつか間違っていると思うが、私は正確には分からない。だから、私のこの機能について誰も助けてくれるはずです。

enter image description here

+0

特定の行のクリックしたボタンを管理して色を変更する必要があります。 –

+0

どこのラベルの色を変更するには – sarosar

+0

@ sarosarあなたは、Googleのボタンのような何かをしたいですか? – Mahesh

答えて

1

私が正しくあなたの質問を得た場合は、UIButtonの状態をtoogleし、それに応じてUILabelの背景色を管理したい:

これはエラー指定されたスクリーンショットです。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 



    kioskStatus = [NSMutableArray arrayWithObjects:@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open", nil]; 

    //arrButtonState holds the data of selection state of button for particular cell 
    //Both array kioskStatus & arrButtonState count will be same all time 
    arrButtonState =[[NSMutableArray alloc]init]; 

    //initially All objects in arrBusttonState Will Be "false" 
    for (int i = 0; i < kioskStatus.count; i++) 
    { 
     [arrButtonState addObject:@"False"]; 

    } 
} 

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

    static NSString *simpleTableIdentifier = @"Cell"; 

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; // Custom cell identifier for the string 
    if (cell == nil) // Check the cell values are nill or not 
    { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; // Initialize the customTableviewCell identifer to the cell. 
    } 

    //Setting Text 
    cell.kioskStat.text = [kioskStatus objectAtIndex:indexPath.row]; 

    //Setting Tag to button 
    cell.detailsButton.tag=indexPath.row; 

    //if previous or default state of button of this particlular cell is false 
    if ([[arrButtonState objectAtIndex:indexPath.row] isEqualToString:@"False"]) { 
     [cell.detailsButton setSelected:FALSE]; 

    } 
    else 
    { 
     [cell.detailsButton setSelected:TRUE]; 
    } 


    if(cell.detailsButton.selected==YES) 
    { 
     cell.kioskStat.textColor=[UIColor greenColor]; //if button is selected the label color to change green 
    } 
    else 
    { 
     cell.kioskStat.textColor=[UIColor blackColor]; //else the label color to change black 
    } 

    //Button Action 
    [cell.detailsButton addTarget:self action:@selector(detailsButtonAction:) forControlEvents: UIControlEventTouchUpInside]; 

    return cell; // returns the cell values to the table view. 
} 


-(void)detailsButtonAction:(UIButton*)sender 
{ 

    //Check that button state is selected or not 
    if (sender.selected) 
    { 
     //if button is already selected turn it to false state 
     [arrButtonState replaceObjectAtIndex:sender.tag withObject:@"False"]; 

    } 
    else 
    { 
     //if button not selected then turn it to selected state 
     [arrButtonState replaceObjectAtIndex:sender.tag withObject:@"True"]; 

    } 

    return [self.tableView reloadData]; //Reload the table view. 
} 
+0

素晴らしいそれは正常です。ありがとう、マヘシュ – sarosar