2012-04-20 71 views
1

我有UITableView ...當用戶點擊行時,另一個屏幕打開。問題是,有時候,我點擊一次,但是多次調用SelectRowAtIndexPath。如何防止呢?iPhone:UITableView,didSelectRowAtIndexPath多次調用

的一個情況下如何重現這種情況是(你甚至可以嘗試重現原生iPhone設置):

  1. 點擊一行,但不左鬆開手指
  2. SLIDE幾下一行向右或由右至左(不只是輕點,你應該滑動)由另一方面在不同的順序下幾行
  3. 鬆開手指

你會看到藍色的選擇是在服務器al行,並且將打開什麼屏幕是隨機的

更新: 在didSelectRow我剛剛啓動了新的控制器,其中viewDidLoad同步開始。 如果一步重現我的方案一步,比同步可以多次啓動

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

如果您不釋放手指,'didSelectRowAtIndexPath''不會被調用。還有另一個委託'willSelectRowAtIndexPath',它在它之前被調用。這些代表不會被多次調用。您可能需要發佈一些代碼來描述您的問題。我相信這是你在代碼中做錯了。 – 2012-04-20 15:59:28

+0

你是對的,如果我不釋放手指,didSelectRowAtIndexPath將不會被調用,但是當我釋放手指時,它會多次調用。這個方法沒什麼特別的。我會在一分鐘內添加更新 – Jim 2012-04-20 16:03:58

回答

1

是的,我發現了同樣的情況。

  1. 點擊一行但不釋放手指。
  2. 繼續按住並輕輕移動手指,直到取消選中行。
  3. 保持第一根手指按下,然後用另一根手指輕敲屏幕幾次。
  4. 鬆開所有手指。

然後你可以看到幾次調用didSelectRowAtIndexPath方法。

我創建了一個測試它的新項目,並使用了下面的代碼。它每次都被複制。

所以我認爲這是一個iOS SDK的bug!

#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 
+6

這不是一個解決方案 – 2013-10-01 14:39:24