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

时间:2015-04-24 06:54:27

标签: ios objective-c uitableview uipangesturerecognizer didselectrowatindexpath

我在我的应用程序中使用ABMenuTableViewCell tableview控制器。我想轻扫didSelectRowAtIndexPat时要拨打UITableViewCell h。

现在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 这是我刷卡的时候 enter image description here

3 个答案:

答案 0 :(得分:1)

您可以修改argument方法以下代码。执行滑动操作后,它会将-bootclasspath "F:\Android SDK\platforms\android-21\android.jar" 显示为swipeGesture

UITableViewCell

希望这对你有所帮助。

答案 1 :(得分:1)

当您尝试实现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];
}

UITapGestureRecognizer方法中添加-commonInit

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

更新_menuVisible内的-updateMenuView:animated:

- (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;
    }
    ...
}

您将无法选择单元格,但据我所知,您不想选择单元格。

答案 2 :(得分:0)

如果您希望在滑动单元格时调用该方法,则应使用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:方法。