【问题标题】:How can i change background color of UIButton on click in UITableviewCell?如何在 UITableviewCell 中单击时更改 UIButton 的背景颜色?
【发布时间】:2015-03-25 08:07:14
【问题描述】:

我有 UITableviewCell,我在单元格中放置了 4 个按钮。当我单击一个按钮时,我需要将其背景颜色更改为红色。

现在我已经为此编写了代码,当我单击一个按钮时,该按钮的背景颜色不会改变,而不是其他行中的相同按钮改变背景颜色。

用例: 1.我在 UITableView 中有 10 行,每个单元格包含 4 个按钮,命名为 “好”、“更好”、“最好”、“最差”。

  1. 当我点击第一行的“良好”按钮时,我希望它的颜色会变为红色。

3.现在如果我点击第一行的“好”按钮,那么它在向下滚动时不会改变颜色,但我可以看到第 4 和第 8 的“好”按钮变为红色。
所以我的代码在改变颜色时出现了一些错误。

请检查我的表格视图代码和按钮点击代码

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int sectionCount;
    if(section==0)
    {
        sectionCount=4;
    }else if(section==1){
        sectionCount=4;

    }else if (section==2){
        sectionCount=3;

    }else if(section==3){
        sectionCount=1;

    }
    return sectionCount;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    sharedManager=[Mymanager sharedManager];
        static NSString *CellIdentifier = @"cell";

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[questioncell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:CellIdentifier];
        }

        cell.excellentButton.tag=indexPath.row;
        cell.goodButotn.tag=indexPath.row;
        cell.fineButton.tag=indexPath.row;
        cell.dorrButton.tag=indexPath.row;
    if(indexPath.section==0){
        cell.question.text=[questions objectAtIndex:indexPath.row];
    } else if(indexPath.section==1){
        cell.question.text=[section1 objectAtIndex:indexPath.row];

    }else if(indexPath.section==2){
        cell.question.text=[section2 objectAtIndex:indexPath.row];

    }else if(indexPath.section==3){
        cell.question.text=[section3 objectAtIndex:indexPath.row];

    }
 return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

按钮点击代码

- (IBAction)goodButton:(id)sender {

    UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
    NSInteger row = button.tag; // recover the row
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
    [sharedManager.ratingDic setValue:@"Good" forKey:key];
    cell.goodButotn.layer.backgroundColor=[[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];

    cell.betterButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
    cell.bestButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
    cell.worstButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];



}

请帮我解决这个问题

【问题讨论】:

    标签: ios objective-c uitableview uibutton


    【解决方案1】:

    UIButton *button = (UIButton *)sender;

    创建一个自定义UITableViewCell 并实现重置所有颜色和设置正确颜色的方法。这是干净的方式。在这里您可以实现更多逻辑并始终拥有正确的相应数据。在这里,您的每个按钮都调用了自己的方法,并且您还有一个 clear 方法。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] )
        {
            [excellentButton addTarget:self action:@selector(clickedExcellentButton) forControlEvents:UIControlEventTouchDown]; // or TouchUp, how ever you like
            [goodButton addTarget:self action:@selector(clickedGoodButton) forControlEvents:UIControlEventTouchDown];
            [fineButton addTarget:self action:@selector(clickedFineButton) forControlEvents:UIControlEventTouchDown];
            [dorrButton addTarget:self action:@selector(clickedWoButton) forControlEvents:UIControlEventTouchDown];
        }
    
        return self;
    }
    
    -(UIColor *)selectionColor
    {
        return [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
    }
    
    -(void)resetSelection
    {
        excellentButton.backgroundColor = [UIColor clearColor];
        goodButton.backgroundColor = [UIColor clearColor];
        fineButton.backgroundColor = [UIColor clearColor];
        dorrButton.backgroundColor = [UIColor clearColor];
    }
    
    -(void)clickedExcellentButton
    {
        [self resetSelection];
        excellentButton.backgroundColor = [self selectionColor];
        NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
        [sharedManager.ratingDic setValue:@"Excellent" forKey:key]; // if you have your sharedManager object here. If you cannot access it from here, you have to forward it or give the cell a reference to it
    }
    
    -(void)clickedGoodButton
    {
        [self resetSelection];
        goodButton.backgroundColor = [self selectionColor];
        NSString *key=[NSString stringWithFormat:@"%ld",(long)indexPath.row];
        [sharedManager.ratingDic setValue:@"Good" forKey:key];
    }
    
    ...
    

    - (IBAction)goodButton:(id)sender {
    
    UIButton *button = (UIButton *)sender; // this is the button that was clicked
    // [...]
    for(UIButton *ctrl in [button.superview subviews]) // button.superview get the view that holds him. Subviews all the others in his layer
    {
        if([ctrl isKindOfClass:[UIButton class]])
        {
            ctrl.backgroundColor = [UIColor clearColor];
        }
    }
    button.backgroundColor = [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
    

    【讨论】:

    • 对自定义单元解决方案进行了更新。也许对你更好
    • 如何更好,你能告诉我吗?
    • 我还没有看到,你已经有一个自定义单元格(questioncell - 顺便说一下,编码约定:类总是有很好的首字母,变量小和静态大写 ^^)。所以你有清洁方式的先决条件;)
    • 你自定义单元格 xD 看看它正在使用的变量,你会很快看到它所属的位置 ^^ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 绝对不是 UIViewControllerUITableViewController 方法,并且我直接访问按钮,没有cell. ;)
    • 我已在自定义单元格类中添加并在 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 中出现错误“控件到达非 void 函数的结尾”
    【解决方案2】:

    我创建了一个自定义表格视图单元格,当按下按钮时,这四个按钮使用相同的 IBAction。然后更改该按钮的背景颜色。它有效。

    表格视图

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil][0];
    }
    return cell;
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    }
    

    按钮点击代码

    - (IBAction)actionButtonPressed:(UIButton *)sender {
    [sender setBackgroundColor:[UIColor redColor]];
    }
    

    【讨论】:

      【解决方案3】:

      您不需要使用按钮层设置背景颜色就足够了(我假设您可以通过 cell.Button 访问您的按钮)

      cell.goodButton.backgroundColor = [[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
      ...
      
      cell.betterButton.backgroundColor = [UIColor clearColor];
      cell. bestButton.backgroundColor = [UIColor clearColor];
      cell. worstButton.backgroundColor = [UIColor clearColor];
      

      【讨论】:

      • 我应该把这段代码放在哪里?内部按钮单击或单元格用于行索引?
      • -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。您将使用 indexPath 来检索单元格。
      • 我没有选择行。请阅读我的问题。我只选择按钮。按钮也有单独的操作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 2020-12-06
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多