Adam G
Adam G

Reputation: 1188

Deselect a button in a table view cell when another is tapped

I have a table view with user posts that can be upvoted and downvoted. I have two custom buttons for the upvote and downvote in the cells, which I use like so:

// in cellForRowAtIndexPath:
[cell.upVote addTarget:self action:@selector(handleThumbsUp:) forControlEvents:UIControlEventTouchUpInside];
[cell.downVote addTarget:self action:@selector(handleThumbsDown:) forControlEvents:UIControlEventTouchUpInside];

//the methods

- (IBAction)handleThumbsUp:(ThumbsUpButton *)sender {

    if (sender.selected == YES) {

        [sender setSelected:NO];

    } else {

        [sender setSelected:YES];

    }

}

- (IBAction)handleThumbsDown:(ThumbsDownButton *)sender {

    if (sender.selected == YES) {

        [sender setSelected:NO];

    } else {

        [sender setSelected:YES];

    }

}

When the "Thumbs Up" button is selected, and the user changes his mind and presses "Thumbs Down", how can I deselect the "Thumbs Up" button in that same cell?

Upvotes: 0

Views: 347

Answers (3)

meth
meth

Reputation: 1885

For simplest solution (with minimum structure change and code) it may be achieved with moving button action methods to your custom cell class. then add actions to upvote and downvote in cellForRowAtIndexPath:

// in cellForRowAtIndexPath:
[cell.upVote addTarget:cell action:@selector(handleThumbsUp:) forControlEvents:UIControlEventTouchUpInside];
[cell.downVote addTarget:cell action:@selector(handleThumbsDown:) forControlEvents:UIControlEventTouchUpInside];

Or you can directly set this methods from Nib file.

and then change the upvote / downvote methods like this.

//the methods

- (IBAction)handleThumbsUp:(ThumbsUpButton *)sender {

    if (sender.selected == YES) {//upvote undone

        [sender setSelected:NO];

    } else {//upvote done

        [self.upVote setSelected:YES];
        [self.downVote setSelected:NO];//delesect downvote
    }

}

- (IBAction)handleThumbsDown:(ThumbsDownButton *)sender {

    if (sender.selected == YES) {//downVote undone

        [sender setSelected:NO];

    } else {

        [self.downVote setSelected:YES];
        [self.upVote setSelected:NO];//deselect upvote

    }

}

also as Josh Caswell stated in his answer, you should have a data for the user's upvote and downvote in your dataModel you fill your cell. This is just a quick answer for this specific case, but to support dataModel changes you can add reference to your model inside your cell and modify it inside this action methods for saving votes.

Upvotes: 0

Pramod Tapaniya
Pramod Tapaniya

Reputation: 1266

By @selector you can access a property of the button not another control of the cell in a button handler method.

So, You must have update whole cell on the button handler method and manually handle the selected state of buttons in cellForRowAtIndexpath delegate method of table.

Upvotes: 0

jscs
jscs

Reputation: 64002

You should have a model that contains the "thumbs up/down" information; you should not be storing it in your views in the form of the button being selected or not.

When one of the buttons is tapped, your controller should update the model and refresh the view based on the state of the model.

(Some kind of binding system would make this easier: ReactiveCocoa is one such option (though it's much more than just model/view bindings); another, much simpler (shameless link to my own free code) is my own UIViewController+WSSDataBindings category.)

Upvotes: 1

Related Questions