Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell setSelected:animated called twice on iPad

I have a custom UITableViewCell on my iPhone app for which I have a custom setSelected:animated method. My app works perfectly on iPhone, however, I started to port my app to iPad. I've copied the exact same storyboard, haven't changed anything, but now my setSelected:animated method is called twice (with the same parameters) when I select my cell. I could "handle" this case by checking if iPad etc. but it would be a bad practice. What could be the reason that it's called once on iPhone but twice on iPad? (both iOS 7.0.3) The table view's properties are exactly the same (I've copied the iPhone storyboard file).

Here is the relevant code:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    isSelected = selected;
    [self setNeedsDisplay];
    if(selected){
        SocialMatchAppDelegate *del = (SocialMatchAppDelegate*)[UIApplication sharedApplication].delegate;
        del.selectedUser = self.user;
        [del.resultViewController performSegueWithIdentifier:@"viewProfile" sender:self];
    }
}
like image 205
Can Poyrazoğlu Avatar asked Jan 08 '14 12:01

Can Poyrazoğlu


1 Answers

I suppose this is a normal behavior if you're using iPad.

In order to stop getting multiple "setSelected:YES" or multiple "setSelected:NO", all you have to do is this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Now, 1 click on any cell gives you:

  • 1 entry of setSelected:YES animated:NO
  • 1 entry of tableView: didSelectRowAtIndexPath:
  • 1 entry of setSelected:NO animated:YES
like image 109
OlDor Avatar answered Sep 22 '22 13:09

OlDor