在每个单元格前面的 UITableView 中显示新图像而不是另一个图像按钮

Posted

技术标签:

【中文标题】在每个单元格前面的 UITableView 中显示新图像而不是另一个图像按钮【英文标题】:Displaying a new image instead of another image button in a UITableView infront of every cell 【发布时间】:2012-09-28 13:57:41 【问题描述】:

一旦单击该按钮,我想在表格视图控制器中的另一个图像按钮顶部添加图像。我在 .h 文件中有 UIButton *按钮。

    CellForRow:
    button=[[UIButton alloc]initWithFrame:CGRectMake(230,0,40,40); 
   [button addTarget:self action:@selector(buttonPressed:withEvent:)                 forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:[UIImage imageNamed:"a"] forState:UIControlStateNormal];
button.tag=indexPath.row;
[cell addSuBView:button];
[cell setIndentationWidth:1];
[cell setIndendationLevel:1];

-(void)buttonPressed:(id) sender withEvent: (UIEvent *) event
UIButton *mybtn=(UIButton*) sender;
[mybtn setImage:[UIImage imageNamed:"b"] forState:UIControlStateNormal];

这样做是在第一个图像之上显示第二个图像。但我想要的是用新图像代替旧图像。

【问题讨论】:

【参考方案1】:
-(void)buttonPressed:(id)sender withEvent:(UIEvent *)event
UIButton *button = (UIButton *)sender;
button.selected = ! button.selected;

                if (button.selected) 
                    [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
                
                else
                    [button setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

                


【讨论】:

它可以工作...但需要双击才能更改图像。 并在后续点击时再次更改为上一张图片

以上是关于在每个单元格前面的 UITableView 中显示新图像而不是另一个图像按钮的主要内容,如果未能解决你的问题,请参考以下文章

uitableview 单元格突出显示

单元格数据未显示在 UITableView 中?

在 UITableView 中自定义每个单元格的分隔符

如何唯一标识 UITableView 中的每个单元格

如何从自定义 UITableview 中检索有关选择单元格的数据

从 UITableView pagingEnabled 获取可见单元格