简体   繁体   English

单击时更改单元格上按钮的图像

[英]Change the image of a button that's on a cell upon click

In my cell i have 3 buttons, when the user clicks on each button the following 3 method gets fired. 在我的单元格中,我有3个按钮,当用户单击每个按钮时,将触发以下3种方法。

在此处输入图片说明

- (void) but1:(id) sender{
    NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);

}

- (void) but2:(id) sender{
    NSLog(@"Touched 2 %ld",(long)[(UIButton *)sender tag]);

}

- (void) but3:(id) sender{
    NSLog(@"Touched 3 %ld",(long)[(UIButton *)sender tag]);

}

Imagine a user clicks on button 1, and then the but1 method should get fired, and also the background image of button 1 should change. 想象一下,用户单击按钮1,然后应触发but1方法,并且按钮1的背景图像也应更改。 How can make this change in the above method. 如何在上述方法中进行此更改。

NB: I am using XIB files. 注意:我正在使用XIB文件。

I was able to access the NIB file that contains the cell . 我能够访问包含该单元格的NIB文件。

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
        [self.contextMenuTableView registerNib:cellNib forCellReuseIdentifier:@"cell"];

Now how can i access the button he touched and change the image ? 现在如何访问他触摸的按钮并更改图像?

UPDATE 更新

This is a table view. 这是一个表格视图。 And in each cell there are 3 buttons, I should be able select and deselect one of these buttons in the table. 在每个单元格中有3个按钮,我应该能够选择和取消选择表格中的这些按钮之一。

For example if only 1 button in the cell can be selected . 例如,如果只能选择单元格中的1个按钮 When the cell is selected i will show an image. 当选择单元格时,我将显示图像。 And when it's not i will show the default image. 如果不是,我将显示默认图像。

在此处输入图片说明

You can change the image of the sender as 您可以将发件人的图像更改为

- (void) but1:(id) sender{
    NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
    [((UIButton *)sender) setImage:[UIImage imageNamed:"image.png"] forState:UIControlStateNormal]
}

Good luck 祝好运

Try something like 尝试类似

          UIImage *btnImage = [UIImage imageNamed:@"image.png"];
          [btnTwo setImage:btnImage forState:UIControlStateNormal];

To Know which button is pressed you can set Tag for every button. 要知道按下了哪个按钮,可以为每个按钮设置标签。 From that tag you will get which button is taped 从该标签中,您将获得粘贴了哪个按钮的信息

In interface Section .. 在界面部分..

int count; 整数计数

In viewDidLoad 在viewDidLoad中

count=0; 计数= 0;

- (IBAction)ButtonClicked:(id)sender {



    NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
    if(count%2==0)
    {
        [((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:@"imageA.png"] forState:UIControlStateNormal];
    }
    else
   {
      [((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:@"imageB.png"] forState:UIControlStateNormal];
   }


    [(UIButton *)sender setTitle:@"" forState:UIControlStateNormal];
    count=count+1;

}
- (void) but1:(id) sender{

  [but1 setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];
  NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);

 }

try this code, this might help you 试试这个代码,这可能对您有帮助

May be this way you can archive it, 也许可以这样存档

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIButton *btn1 = (UIButton *) [cell viewWithTag:1] ;
    UIButton *btn2 = (UIButton *) [cell viewWithTag:2];
    UIButton *btn3 = (UIButton *) [cell viewWithTag:3];
    [btn1 setTag:((indexPath.row*10)+1)];
    [btn1  setTag:((indexPath.row*10)+2)];
    [btn3 setTag:((indexPath.row*10)+3)];
    [btn1 addTarget:self action:@selector(but1:) forControlEvents:UIControlEventTouchDown];
    [btn2 addTarget:self action:@selector(but2:) forControlEvents:UIControlEventTouchDown];
    [btn3 addTarget:self action:@selector(but3:) forControlEvents:UIControlEventTouchDown];
    return cell;
}

- (void) but1:(UIButton *) sender{
    NSLog(@"Touched 1 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

- (void) but2:(UIButton *) sender{
    NSLog(@"Touched 2 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

- (void) but3:(UIButton *) sender{
    NSLog(@"Touched 3 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

Enjoy Coding !! 享受编码!

you can also use this 你也可以用这个

button1.tag = 0;
button2.tag = 1;
button3.tag = 2;
[button1 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button3 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];

Add a common selector( buttonClicked: ) to all the buttons and implement the selector as follows 向所有按钮添加一个公共选择器( buttonClicked:并实现选择器,如下所示

- (void)buttonClicked:(id)sender
{
    [button1 setSelected:NO];
    [button2 setSelected:NO];
    [button3 setSelected:NO];

    if ([sender isKindOfClass:[UIButton class]]) 
        [sender setSelected:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM