UITextView 子类点击 didSelectRowAtIndexPath

发布于 2025-01-04 20:36:57 字数 899 浏览 1 评论 0原文

我有一个 UITextView 子类,它有一个位于 UITableViewCell 内的 NSIndexPath 属性。当您点击它时,我希望能够调用 didSelectRowAtIndexPath

这就是我所拥有的:

UITapGestureRecognizer *singleFingerTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
[theTextView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;

....

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    CustomTextView *theTextView = (CustomTextView *)touch.view;

    NSLog(@"%@", theTextView.indexPath);


    return YES;

}

我从这个问题中看到,我什至可能不得不分解我的< code>didSelectRowAtIndexPath 逻辑,这很好。我已经知道所点击的视图的索引路径。

从手势识别器方法中调用此 tableView 方法(或者 didSelectRowAtIndexPath 会做什么)的正确方法是什么?

I have a UITextView subclass that has an NSIndexPath property that is inside a UITableViewCell. When you tap on it, I'd like to be able to call didSelectRowAtIndexPath.

Here's what I have:

UITapGestureRecognizer *singleFingerTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
[theTextView addGestureRecognizer:singleFingerTap];
singleFingerTap.delegate = self;

....

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    CustomTextView *theTextView = (CustomTextView *)touch.view;

    NSLog(@"%@", theTextView.indexPath);


    return YES;

}

I see from this question, I may even have to break up my didSelectRowAtIndexPath logic, which is fine. And I already know the index path of the view that was tapped.

What is the proper way to call this tableView method (or what didSelectRowAtIndexPath would do) from within the gesture recognizer method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-01-11 20:36:57

我在这里回答了类似的问题。基本上,当您调用时:

[tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];

tableview不会在tableview的委托上触发tableView:didSelectRowAtIndexPath:,因此此外您还被迫直接调用(并且由于您在委托方法中编写了代码,因此没有问题):

[tableView.delegate tableView:tableView didSelectRowAtIndexPath:path];

此外,您为操作传递的 nil 实际上并不是手势识别器的工作方式:

[[UITapGestureRecognizer alloc] initWithTarget:self action:nil];

通常您会做这样的事情:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];

这基本上告诉手势识别器调用tapRecognized: 收到点击时的方法。所以基本上将其全部包装起来(我假设这是在 UITableViewController 中或具有名为 tableView 属性的对象中,因为 UITableViewController 具有默认情况下)。

-(void)tapRecognized:(UITapGestureRecognizer *)tapGR{
    // there was a tap
    CustomTextView *theTextView = (CustomTextView *)tapGR.view;
    NSIndexPath *path = theTextView.indexPath;
    [self.tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:path];
}

I answered a similar question here. Basically when you call:

[tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];

the tableview doesn't fire the tableView:didSelectRowAtIndexPath: on the tableview's delegate, so in addition you are forced to directly call (And since you wrote the code in the delegate method there's no problem):

[tableView.delegate tableView:tableView didSelectRowAtIndexPath:path];

Also your passing nil for the action is really not how gesture-recognizers where mean to work:

[[UITapGestureRecognizer alloc] initWithTarget:self action:nil];

Generally you would do something like this:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];

That basically tells the gesture recognizer to call the tapRecognized: method when it receives a tap. So basically to wrap it all up (I'm assuming this is in a UITableViewController or an object with a property named tableView as UITableViewControllers have by default).

-(void)tapRecognized:(UITapGestureRecognizer *)tapGR{
    // there was a tap
    CustomTextView *theTextView = (CustomTextView *)tapGR.view;
    NSIndexPath *path = theTextView.indexPath;
    [self.tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:path];
}
赏烟花じ飞满天 2025-01-11 20:36:57

如果您需要执行 X 来响应手势和表视图行上的点击,只需创建一个方法 X 并从手势识别器方法中调用该方法,如下所示以及来自您的 didSelectRowAtIndexPath 表视图委托方法。

当然,理论上

[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

是可行的,但我认为至少可以说,自己调用委托方法是一种不好的风格。

If you need to do X both in response to a gesture and to a tap on a table view row, simply create a method X and call that from your gesture recognizer method as well as from your didSelectRowAtIndexPath table view delegate method.

Of course, theoretically,

[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

would work, but I think calling the delegate method yourself is bad style to say the least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文