HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UIGestureRecognizer和UITableViewCell问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我附加一个UISwipeGestureRecognizer到一个UITableViewCell在cellForRowATindexPath:方法如下:

- (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方法在成功滑动时总是调用两次。我最初认为这是因为手势开始和结束,但如果我退出手势识别器本身,他们都处于“已结束”状态:

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

我真的不知道为什么。我试图明显地检查结束状态,但这是没有帮助,因为他们都以“结束”反正…任何想法?

解决方法

而不是直接将手势识别器添加到单元格,可以将其添加到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问题全部内容,希望文章能够帮你解决ios – UIGestureRecognizer和UITableViewCell问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。