lucas
lucas

Reputation: 443

UIButton in UITableViewCell did not call the action

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];

}`

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

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?

Thanks.

Upvotes: 1

Views: 2043

Answers (2)

Vaibhav Tekam
Vaibhav Tekam

Reputation: 2344

7KV7 is right, you need to send the button object to the method in order to change the image of it.

Have a look at this thread

how-to-select-particular-check-box-in-tableview-which-is-inserted-in-table-cell

Upvotes: 0

visakh7
visakh7

Reputation: 26400

Change this - (void)toggleImage { NSLog(@"test"); } to

- (void)toggleImage:(id)sender { NSLog(@"test"); }and see if it works.

Upvotes: 1

Related Questions