ios - 用于更改标签背景颜色的表格单元格按钮操作

标签 ios objective-c uitableview

我需要通过在按钮操作中使用 touchup 来更改 tableview 单元格中的标签背景颜色。我在表格 View 中创建了自定义单元格,然后将 20 行添加到单元格中(使用 NSMutableArray),然后我创建了 UIButton 并以编程方式实现按钮操作( TouchUPInside Action )。

我的情况是“当我单击按钮时,该特定索引标签(特定行)中的标签背景颜色更改为绿色。但在我的代码中它反射(reflect)了所有行中的操作。它的标签颜色必须更改在所有行中。”

这是我的代码:

//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

最佳答案

如果我正确地回答了你的问题,你想切换 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.
}

关于ios - 用于更改标签背景颜色的表格单元格按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867182/

相关文章:

swift - tableview 中的 Collectionview 导致 2 列出现问题

iphone - MPMusicPlayerController 可以播放本地资源的音乐吗?

ios - 夹在 willBeginEditingRowAtIndexPath 和 didEndEditingRowAtIndexPath 之间

ios - CMDeviceMotion 的磁场属性不正确?

objective-c - 调用 RCTDirectEventBlock 不会触发 JS 代码

objective-c - iOS – 回调的设计决策(使用 NSNotifications 或 AppDelegate)

ios - 数组索引超出范围

ios - HTTPNetStreamInfo::_readStreamClientCallBack(__CFReadStream*, unsigned long) 增加内存分配

ios - 在 iOS 中以编程方式在标签栏上添加图像

iphone - 如何同时从 UITableView 插入和删除一行