gpt4 book ai didi

ios - UIGestureRecognizer 和 UITableViewCell 问题

转载 作者:IT王子 更新时间:2023-10-29 07:38:54 24 4
gpt4 key购买 nike

我将 UISwipeGestureRecognizer 附加到 cellForRowAtIndexPath: 方法中的 UITableViewCell,如下所示:

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

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell.contentView addGestureRecognizer:gesture];
[gesture release];
}
return cell;
}

但是,didSwipe 方法总是在成功滑动时被调用两次。我最初认为这是因为手势开始和结束,但如果我注销 gestureRecognizer 本身,它们都处于“已结束”状态:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

NSLog(@"did swipe called %@", gestureRecognizer);
}

控制台:

2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>

我真的不知道为什么。我显然尝试检查 Ended 状态,但这无济于事,因为无论如何它们都是“Ended”...有什么想法吗?

最佳答案

您可以将手势识别器添加到 viewDidLoad 中的 tableview,而不是直接将手势识别器添加到单元格。

didSwipe-方法中,您可以确定受影响的 IndexPath 和单元格,如下所示:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {

if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
// ...
}
}

关于ios - UIGestureRecognizer 和 UITableViewCell 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com