2016-04-26 59 views
1

我需要在按鈕操作中使用touchup來更改tableview單元格中的標籤背景顏色。我在表格視圖中創建了自定義單元格,然後將20行添加到單元格中(使用NSMutableArray),然後創建了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你想做點像點擊按鈕? – Mahesh

回答

1

,如果我得到你的問題正確,你想的toogle的UIButton狀態,並相應地管理的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

太棒了。它工作很好。謝謝Mahesh – sarosar

相關問題