简体   繁体   中英

iPhone: UITableView, didSelectRowAtIndexPath called several times

I have UITableView...when user tap on row, another screen is opened. The problem is, that sometimes, I tap once, but didSelectRowAtIndexPath calls several times. How to prevent that ?

The one case how to reproduce that situation is (you even can try to reproduce that on native iPhone settings):

  1. Tap one row but do not release finger
  2. SLIDE few next rows from left to right or from right to left (not just tap, you should slide) next few rows in different order by other hand
  3. Release finger

You will see that blue selection is on several rows, and what screen will be opened is random

UPDATE: In didSelectRow I just started new controller, where in viewDidLoad synchronization begin. And if to reproduce my scenario step by step, than synch can be started several times

  - (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secondViewController =
        [SecondViewController alloc] init];
    [self.navigationController 
        pushViewController:secondViewController animated:YES];
    [secondViewController release];
  }

Yes, I find the same situation.

  1. Tap one row but do not release finger.
  2. Keep pressing and moving the finger slightly until the row deselected.
  3. Keep the first finger pressing, and tap the screen some times by another finger.
  4. Release all fingers.

Then you can see didSelectRowAtIndexPath method called several times.

I created a new project for test it, and just used the following code. It was reproduced in every times.

So I think it is a bug of iOS SDK !

#import "SPViewController.h"

@interface SPViewController ()
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation SPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Test Cell %d", indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s %@", __FUNCTION__, indexPath);
}

@end

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