点击单元格时从 uitableviewcell 类调用 uipangesture

Posted

技术标签:

【中文标题】点击单元格时从 uitableviewcell 类调用 uipangesture【英文标题】:Calling uipangesture from uitableviewcell class when tapped a cell 【发布时间】:2015-04-24 06:54:26 【问题描述】:

我在我的应用程序中使用ABMenuTableViewCell tableview 控制器。当我刷UITableViewCell时,我想打电话给didSelectRowAtIndexPath。

现在didSelectRowAtIndexPath 仅在我点击单元格时执行,即使我滑动它也想调用它。这是我的didSelectRowAtIndexPathcellforRowAtIndexPath 方法代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

        UILabel *likes;
        UILabel *downloads;
        static NSString *CellIdentifier = @"Cell";
        ABMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        
            cell = [[ABMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"acc_arrow_back.png"]];
            arrow.frame = CGRectMake(300, 50, 5, 12);
            arrow.image = [arrow.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [arrow setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:arrow];

UIImageView *likes_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"social.png"]];
            likes_img.frame = CGRectMake(15, 80, 15, 15);
            likes_img.image = [likes_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [likes_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:likes_img];

            likes =[[UILabel alloc]initWithFrame:CGRectMake(33, 78, 80, 20)];
            likes.tag = 1001;    // set a tag for this View so you can get at it later
            likes.textColor=[UIColor darkGrayColor];
            likes.font=[UIFont fontWithName:@"Helvetica" size:10.0f];
            likes.text=[[rssOutputData objectAtIndex:indexPath.row]xmllikes];
            [cell.contentView addSubview:likes];
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];

            UIImageView *downloads_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"download.png"]];
            downloads_img.frame = CGRectMake(55, 79, 15, 15);
            downloads_img.image = [downloads_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
            [downloads_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
            [cell.contentView addSubview:downloads_img];

            downloads =[[UILabel alloc]initWithFrame:CGRectMake(73, 78, 80, 20)];
            downloads.tag = 1002;    // set a tag for this View so you can get at it later
            downloads.textColor=[UIColor darkGrayColor];
            downloads.font=[UIFont fontWithName:@"Helvetica" size:10.0f];
            downloads.text=[[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
            [cell.contentView addSubview:downloads];
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        
        else
        
            // use viewWithTag to find lblNombre in the re-usable cell.contentView
            likes = (UILabel *)[cell.contentView viewWithTag:1001];
            downloads = (UILabel *)[cell.contentView viewWithTag:1002];

        
        cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmlsinger];
        cell.detailTextLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
        // custom menu view
        NSString *nibName = @"ABCellMailStyleMenuView";
        ABCellMenuView *menuView = [ABCellMenuView initWithNib:nibName bundle:nil];
        menuView.delegate = self;
        menuView.indexPath = indexPath;
        cell.rightMenuView = menuView;
        return cell;



   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
   

这些是单元类 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer中的方法和 - (void)swipeGesture:(UIPanGestureRecognizer *)gesture 这是我刷卡的时候

【问题讨论】:

您能否添加您的屏幕截图,以显示您的UITableView 显示方式? 你为什么要那个?让ABMenuTableViewCell 在滑动时显示菜单不是重点吗? @JakubVano 感谢您的回复。是的,它正在显示,但现在它显示的是当一个单元格被滑动而不是点击时。我想同时调用这两个东西,比如如果我滑动它也会调用 didSelectRowAtIndexPath 因为我测试了ABMenuTableViewCell,它工作正常。我认为有一些内容重叠。 @None 我添加了截图 【参考方案1】:

您可以修改swipeGesture 方法将以下代码。一旦您执行滑动操作,它会将UITableViewCell 显示为selected

- (void)swipeGesture:(UIPanGestureRecognizer *)gesture 
    if (gesture.state == UIGestureRecognizerStateBegan) 
        NSInteger direction;

        // find swipe direction
        CGPoint velocity = [gesture velocityInView:self];
        if (velocity.x > 0) 
            // towards right - hide menu view
            direction = ABMenuUpdateHideAction;
        
        else 
            // towards left - show menu view
            direction = ABMenuUpdateShowAction;
        


        UITableView* tableView = (UITableView*)self.superview.superview;

        CGPoint swipeLocation = [gesture locationInView:tableView];
        NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:swipeLocation];

        [tableView selectRowAtIndexPath:swipedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];


        [self updateMenuView:direction animated:YES];
    

希望对您有所帮助。

【讨论】:

谢谢这个帮助很大,但是当我点击时菜单仍然没有打开,但是现在打开的菜单在我点击另一个单元格时关闭 所以你不想关闭之前刷过的单元格吧? 是的,我确实想关闭它。但我也想在点击单元格时打开菜单 好的,如果这不可能的话,你能告诉我当菜单出现时如何调用我的视图控制器的方法! 使用Delegate,可以调用UIViewController的方法。【参考方案2】:

当您尝试实现ABMenuTableViewCell 不支持的行为时,您需要编辑它的源代码:

添加_tapGesture_menuVisible实例变量:

@implementation ABMenuTableViewCell 
    CGRect _rightMenuViewInitialFrame;
    UIPanGestureRecognizer *_swipeGesture;
    UITapGestureRecognizer *_tapGesture;
    BOOL _menuVisible;

实现-tapGesture:方法:

- (void) tapGesture:(UITapGestureRecognizer*)gesture 
if (_menuVisible)
    [self updateMenuView:ABMenuUpdateHideAction animated:YES];
else
    [self updateMenuView:ABMenuUpdateShowAction animated:YES];

-commonInit方法中添加UITapGestureRecognizer

- (void) commonInit 
    _swipeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    _swipeGesture.delegate = self;
    [self addGestureRecognizer:_swipeGesture];

    _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    _tapGesture.delegate = self;
    [self addGestureRecognizer:_tapGesture];

-updateMenuView:animated: 内更新_menuVisible

- (void)updateMenuView:(ABMenuUpdateAction)action animated:(BOOL)animated 
    ...

    switch (action) 
        case ABMenuUpdateShowAction:
            menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame) - initialWidth, .0, initialWidth, CGRectGetHeight(self.contentView.frame));
            _menuVisible = YES;
            break;

        case ABMenuUpdateHideAction:
            menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame), .0, .0, CGRectGetHeight(self.contentView.frame));
            _menuVisible = NO;
            break;

        default:
            break;
    
    ...

您将无法选择单元格,但据我了解,您不想这样做。

【讨论】:

谢谢。我检查了这段代码,但它第一次打开菜单然后我不得不通过滑动它来关闭它。但是我第二次点击它不会打开第三次不会打开然后第四次尝试菜单再次打开 我编辑了我的答案,所以它也隐藏了点击菜单【参考方案3】:

如果您希望在滑动单元格时调用该方法,您应该使用UISwipeGestureRecognizer 而不是UIPanGestureRecognizer

在您的viewDidLoad: 中,设置手势识别器。

// Create a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];

// Add it to the table view
[self.tableView addGestureRecognizer:recognizer];

处理滑动。

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer

    // Get location of the swipe
    CGPoint location = [gestureRecognizer locationInView:self.tableView];

    //Get the corresponding index path within the table view
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

    // Check if index path is valid
    if (indexPath)
    
        // Select the cell at the indexPath
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

这应该调用tableView: didSelectRowAtIndexPath: 方法。

【讨论】:

谢谢我试过这个,但它甚至没有触发 handleSwipeLeft 方法 您是否尝试将delaysTouchesBegan 属性设置为YES?

以上是关于点击单元格时从 uitableviewcell 类调用 uipangesture的主要内容,如果未能解决你的问题,请参考以下文章

编辑单元格时自定义 UITableViewCell 反向缩进

点击时从 UICollectionView 中的单元格获取图像

选择时从 UITableViewCell 中消失的视图

点击 SwiftUI 时从列表中删除

通过点击单元格内的图像从 UITableViewCell 中分离

只有在 iOS 7 中点击单元格时,tableview 单元格中的 labeltext 才会消失