简体   繁体   中英

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

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

Where did - (void)setSelected:(BOOL)selected call in your source?

If it is in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Using this instead

[tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM