UITextView 子类点击 didSelectRowAtIndexPath
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里回答了类似的问题。基本上,当您调用时:
tableview不会在tableview的委托上触发
tableView:didSelectRowAtIndexPath:
,因此此外您还被迫直接调用(并且由于您在委托方法中编写了代码,因此没有问题):此外,您为操作传递的
nil
实际上并不是手势识别器的工作方式:通常您会做这样的事情:
这基本上告诉手势识别器调用
tapRecognized:
收到点击时的方法。所以基本上将其全部包装起来(我假设这是在UITableViewController
中或具有名为tableView
属性的对象中,因为UITableViewController
具有默认情况下)。I answered a similar question here. Basically when you call:
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):Also your passing
nil
for the action is really not how gesture-recognizers where mean to work:Generally you would do something like this:
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 aUITableViewController
or an object with a property namedtableView
asUITableViewController
s have by default).如果您需要执行
X
来响应手势和表视图行上的点击,只需创建一个方法X
并从手势识别器方法中调用该方法,如下所示以及来自您的didSelectRowAtIndexPath
表视图委托方法。当然,理论上
是可行的,但我认为至少可以说,自己调用委托方法是一种不好的风格。
If you need to do
X
both in response to a gesture and to a tap on a table view row, simply create a methodX
and call that from your gesture recognizer method as well as from yourdidSelectRowAtIndexPath
table view delegate method.Of course, theoretically,
would work, but I think calling the delegate method yourself is bad style to say the least.