本文介绍了UITableViewCell中的UIButton没有调用操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell中有一个UIButton,当它被选中时,我想切换它的图像.下面是我的代码:

I have a UIButton in the UITableViewCell which i would like to toggle it's image when it got selected. Below is my code:

- (UITableViewCell *)tableView:(UITableView *)todoTable cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [self.todoTable dequeueReusableCellWithIdentifier:CellIdentifier];

if(!cell)
    cell = [self getCellContentView:CellIdentifier];


UIImageView *checkboxView = (UIImageView *)[cell viewWithTag:1];

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 30.0, 30.0)];

UIImage *uncheckedButtonImage = [UIImage imageNamed:@"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];

[button addTarget:self action: @selector(toggleImage:) forControlEvents: UIControlEventTouchUpInside];

[checkboxView addSubview:button];

}`

,我的toggleImage方法就是:-(void)toggleImage{NSLog(@"test");}

and my toggleImage method is just:- (void)toggleImage{ NSLog(@"test");}

但是,每当我单击按钮本身时,该单元格的行就会在didSelectRowAtIndexPath处调用,而我不希望这样做,我希望按钮调用toggleImage方法.我是否以错误的方式设置了按钮的动作?

But whenever i click on the button itself, the row of cell gets called at didSelectRowAtIndexPath which i don't want, i want the button call the toggleImage method. Is it i set my button's action the wrong way?

谢谢.

推荐答案

更改此-(void)toggleImage {NSLog(@"test");}

-(void)toggleImage:(id)sender {NSLog(@"test");} ,看看是否可行.

这篇关于UITableViewCell中的UIButton没有调用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:24