首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIGestureRecognizer in UITableViewCell

UIGestureRecognizer in UITableViewCell
EN

Stack Overflow用户
提问于 2015-04-20 11:23:32
回答 1查看 161关注 0票数 0

我想把UIGestureRecognizer添加到UITableViewCell中。

当我在panAction:类中调用CustomCell时,它将工作。

但是我想在ViewController类中调用该方法,在这种情况下,它将无法工作。

如何修复它以工作panAction:

代码语言:javascript
复制
- (void)viewDidLoad
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *nibName = @"Cell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:nibName];
    if (!cell) {
        cell = (CustomCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nibName];
    }
    return cell;
}

- (void)setupGesture
{
    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    CustomCell* cell = (CustomCell*)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [cell addGestureRecognizer:panRecognizer];
}

- (void)panAction:(UIPanGestureRecognizer *)sender
{
    CGPoint location = [sender translationInView:sender.view];    
    NSLog(@"%f", location);
    [sender setTranslation:CGPointZero inView:sender.view];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-20 11:30:29

在上面发布的代码中,您不需要调用setupGesture。这样你的手机就不会做出手势了。另外,您应该添加create并将panRecognizer添加到tableview:cellForRowAtIndexpath:中的单元格中,如下所示:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *nibName = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:nibName];

    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [cell addGestureRecognizer:panRecognizer];

    return cell;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29746520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档