简体   繁体   English

UITableViewCell如何检测用户向左或向右滑动?

[英]UITableViewCell How can I detect that the user has swiped left or Right?

我正在使用SWTableViewCell ,我想知道用户向左或向右滑动时触发的操作。

you have to add Gesture Recognizer in you cellForRowAtIndexPath 您必须在cellForRowAtIndexPath中添加Gesture Recognizer

 UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
[swRight setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swRight];

UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swLeft];

and then its selector method 然后是它的选择器方法

-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}

-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // your code
    }
}

Try this code it will works for you: 试试下面的代码,它将为您工作:

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

    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [cell addGestureRecognizer:swipe];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];


return cell;
    }


    - (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
    {
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
        {
            UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
            NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
            //..
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检测用户何时在 WKWebView 上向左或向右滑动来回 - Detect when user has swiped left or right to go back or forth on WKWebView 如何检测对象是否已在Sprite Kit中刷过 - How do I detect if object has been swiped in sprite kit 在 iOS 中,如何检测 UITableview 中的单元格是否正在被刷卡? - In iOS, how can I detect if a cell in UITableview is in the process of being swiped? 如何 go 关于可以向左/向右和向上/向下滑动的视图? - How to go about a view which can be swiped left/right AND up/down? 仅使用情节提要,如何创建可以使用UIScrollView左右滚动的UITableViewCell? - With Storyboards only, how do I create a UITableViewCell that can scroll left and right using UIScrollView? 如何来回移动UITableViewCell以显示它可以刷? - How to move UITableViewCell back and forth to show it can be swiped? 如何将UITableViewCell的contentView重新放置在左侧或右侧? - How do I reposition a UITableViewCell's contentView to the left or right? 如何检测用户是否在UIMenuController中点击了格式化按钮? - How can I detect that a user has tapped a formatting button in a UIMenuController? 如何在UITableViewCell中向左或向右对齐UILabel - How to align a UILabel left or right in a UITableViewCell UITableViewCell 左右边距 - UITableViewCell margin left and right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM