我有一个滑动识别器附加到我的表格 View 中的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//swipe recognition
UISwipeGestureRecognizer *g = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)];
[cell addGestureRecognizer:g];
[g release];
}
// Configure the cell...
[[cell textLabel] setText:[NSString stringWithFormat:@" number %d", indexPath.row]];
return cell;
}
滑动功能是
- (void)cellWasSwiped:(UIGestureRecognizer *)g {
NSLog(@"sunt in cellWasSwiped");
swipeViewController *svc = [[swipeViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
[svc release];
}
通过引入断点,我看到我的滑动功能被调用了 2 次,并且有两个相同的 View Controller 被推送到我的导航 Controller 上。为什么我滑动单元格时会调用两次滑动功能?
最佳答案
您的cellWasSwiped
可以在UIGestureRecognizer
状态改变时被多次调用。您需要检查属性 state
,当用户完成他的操作时,它将是 UIGestureRecognizerStateEnded
。检查状态 UIGestureRecognizerStateFailed
和 UIGestureRecognizerStateCancelled
也很好。
关于iphone - 为什么我的函数被调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622667/