ios - 更改 UITableView 中的 UIButton 图像 UIButton 单击

标签 ios objective-c iphone uitableview

在 UITableView cellForRowAtIndexPath 中创建一个按钮和一个图像。为该按钮编写操作单击以更改按钮图像。这里是代码。

ButtonImage = [UIImage imageNamed:@"work_exe_old.png"];    
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton addTarget:self  action:@selector(select_id:)forControlEvents:UIControlEventTouchDown];

- (IBAction)select_id:(id)search
{
    UIButton *tempBtn =(UIButton *) search;
    [tempBtn setImage:[UIImage imageNamed:@"work_exe.png"]    forState:UIControlStateNormal];
}

单击按钮图像已更改 (work_exe.png)。再次单击相同的按钮意味着图像想要更改 (work_exe_old.png) 就像 tableview 中的复选框一样。我怎样才能做到这一点

最佳答案

您可以更好地定制您的代码,而不是以这种方式进行。检查下面的代码:

ButtonImage = [UIImage imageNamed:@"work_exe.png"];
ButtonImageSelected = [UIImage imageNamed:@"work_exe_old.png"];

myButton = [UIButton buttonWithType:UIButtonTypeCustom];

myButton.frame = CGRectMake(250,10,34,34);

myButton.tag=22;

[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];

[myButton setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];

[myButton addTarget:self action:@selector(select_id:)forControlEvents:UIControlEventTouchDown];

你可以像这样实现这个选择器方法:

-(IBAction)select_id:(UIButton *) tempBtn {

  [tempBtn setSelected:! tempBtn.selected];
}

关于ios - 更改 UITableView 中的 UIButton 图像 UIButton 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26795866/

相关文章:

ios - Xamarin - iPhone 分发

ios - Collection View 不使用自定义对象显示单元格

iphone - 为 MKMapView 设置缩放级别

iphone - Objective-C 中用于存储和访问程序范围变量的单例。几个问题

c++ - Cocos2d replaceScene 不释放旧场景

ios - 在移动设备上进行离线反向地理编码的技术?

ios - 如何创建 PhoneGap 插件 iOS

ios - 我们可以将 C++ 文件添加到 iOS 项目中吗?

ios - 在 NativeScript 5.4 插件中使用静态 iOS 库

ios - 如何将导航 Controller 添加到已编程的选项卡栏