Uitableview 向TableViewCell添加手势

Uitableview 向TableViewCell添加手势,uitableview,uiswipegesturerecognizer,Uitableview,Uiswipegesturerecognizer,我正在尝试将滑动手势识别器添加到tableViewCell,但它不起作用 这是我创建手机的方式: CellIdentifier = @"EventsSentCell"; nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"EventsSentCell" owner:self options:nil]; EventsSentCell *cell = [[EventsSentCell alloc] init]; cell = (EventsS

我正在尝试将滑动手势识别器添加到tableViewCell,但它不起作用

这是我创建手机的方式:

CellIdentifier = @"EventsSentCell";
    nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"EventsSentCell" owner:self options:nil];

EventsSentCell *cell = [[EventsSentCell alloc] init];
cell = (EventsSentCell *)[nibObjcet objectAtIndex:0];
这就是我的单元格在.m文件中的启动方式:

-(id)init{
    self = [super init];
    if (self) {
        leftSwipe = [[UISwipeGestureRecognizer alloc] init];
        leftSwipe.direction= UISwipeGestureRecognizerDirectionLeft;
        [leftSwipe addTarget:self action:@selector(swipedLeft)];
        [self addGestureRecognizer:leftSwipe];
    }
    return self;
}
这就是我在.h文件中声明手势识别器的方式:

@property (nonatomic,strong) IBOutlet UISwipeGestureRecognizer *leftSwipe;
但是由于某种原因,我的方法没有被调用

有什么想法吗

谢谢

我已尝试输入以下代码:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    NSLog(@"%@",gestureRecognizer);
    return YES;
}
向左滑动后得到的结果是:

<UILongPressGestureRecognizer: 0xa9d99a0; state = Possible; view = <UITableViewCellContentView 0xa9d8ce0>; target= <(action=_longPressGestureRecognized:, target=<EventsSentCell 0xa9d8bb0>)>>

在回答实际问题之前,让我指出代码中的一些其他问题

EventsSentCell *cell = [[EventsSentCell alloc] init];
cell = (EventsSentCell *)[nibObjcet objectAtIndex:0];
首先,这两条线没有意义。您正在分配和初始化没有nib的EventSentCell实例。执行此操作后,您将覆盖单元格以指向loadNibNamed:初始化的实例。您可以将其简化为EventsSentCell=EventsSentCell*nibObject[0]; 但即使经过这些优化,这仍然不是实现CellForRowatineXpath:的推荐方法。您应该在viewDidLoad中使用Registernb:forCellReuseIdentifier:然后使用 dequeueReusableCellWithIdentifier:forIndexPath:获取一个单元格,而不用自己完全加载nib

其次,

您将此属性声明为IBOutlet,但据我所知,您只在代码中设置它,更具体地说是在init方法中。你完全可以不使用IB插座


这个init方法可能也是您的问题的原因。使用loadNibNamed实例化视图时,将调用initWithCoder:而不是init。实现您的自定义初始化代码在这里添加手势识别器,它应该可以正常工作。

您的“init”方法没有被调用,因此手势识别器没有被设置

您可以尝试在awakeFromNib中进行初始化,但无论如何,您的单元格创建看起来很不传统

假设您正在使用一个带有Xib文件的自定义单元格类,下面是我将如何做到这一点的

创建EventsSentCell对象.m和.h文件

创建一个xib文件EventsSentCell.xib

在xib文件中,删除默认的顶级视图,并将其替换为UITableViewCell,您可以从对象库中拖出一个。在identity inspector中,将其类更改为EventsSentCell

在表viewController的viewDidLoad中

在cellForRowAtIndexPath方法中:

UITableViewCell *cell = 
       [tableView dequeueReusableCellWithIdentifier:@"EventsSentCell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] 
                        initWithStyle:UITableViewCellStyleDefault   
                      reuseIdentifier:@"EventsSentCell"];
  }
在EventsSentCell.m中,从-awakeFromNib触发初始化代码

您的初始化代码:

    leftSwipe = [[UISwipeGestureRecognizer alloc] init];
    leftSwipe.direction= UISwipeGestureRecognizerDirectionLeft;
    [leftSwipe addTarget:self action:@selector(swipedLeft)];
    [self addGestureRecognizer:leftSwipe];
就这样吧

您将获得UILongPress手势识别器:。。。响应您的手势识别器委托方法,因为这是Apple提供的内置手势识别器,其委托设置为单元格。当代码正常工作时,如果您还将手势识别器的委托设置为单元格leftSwipe.delegate=self,您将看到UISWIPgestureRecognitor的simlilar日志:

还需要注意的是,UILongPressGestureRecognitor的视图不是单元格,而是单元格的内容视图。这是单元格视图层次结构的superview,您应该将单元格的所有内容附加到它。虽然将手势识别器连接到手机时可以工作,但我建议如下:

        [self.contentView addGestureRecognizer:leftSwipe];
然后,该手势将正确遵循单元格的视图层次

    leftSwipe = [[UISwipeGestureRecognizer alloc] init];
    leftSwipe.direction= UISwipeGestureRecognizerDirectionLeft;
    [leftSwipe addTarget:self action:@selector(swipedLeft)];
    [self addGestureRecognizer:leftSwipe];
        [self.contentView addGestureRecognizer:leftSwipe];