如何区分UITableViewCell的哪个部分已被点击 [英] how can I distinguish which part of UITableViewCell has been clicked
问题描述
我使用自定义 UITableViewCell
创建了 UITableView
。我的单元格左侧包含一个 UIImageView
,右侧包含 UITextView
。
I have created a UITableView
with a custom UITableViewCell
. My cell includes one UIImageView
on the left and UITextView
on the right.
在 UITableViewController
内,我在tableview中设置了图像和文本 cellForRowAtIndexPath
。
Inside UITableViewController
, I set both image and text in tableview cellForRowAtIndexPath
.
一切都很好但现在我需要实现 didSelectRowAtIndex
我需要区分是否 UIImageView $ c $已单击c>或
单元格的UITextView
。
Everything shows fine but now I need to implement didSelectRowAtIndex
and I need to distinguish if UIImageView
or UITextView
of the cell has been clicked.
假设图像单击表示删除操作,其余的单元格编辑操作。
Let's say, image clicking is representing delete action and the rest of the cell editing action.
推荐答案
您可以在表格中添加一个,而不是将手势识别器添加到每个单独的单元格中。查看并确定从用户触摸点选择的单元格,然后确定用户是否触摸了图像或单元格。
Rather than adding the gesture recognisers to each individual cell, you can add one to the table view and determine which cell was selected from the point of the users touch, and then determine if the user touched the image or the cell.
首先确保您的控制器采用了UIGestureRecognizerDelegate协议。
First make sure your controller adopts the UIGestureRecognizerDelegate protocol.
@interface MyTableViewController() <UIGestureRecognizerDelegate>
@end
然后添加 UIGestureRecognizer $ c $当视图加载时,c>到
UITableView
。
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.delegate = self;
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:singleTap];
}
此委托方法确定 handleTap:应该执行code>方法。如果它可以从用户触摸中找到
indexPath
,则返回 YES
否则返回否
。
This delegate method determines if the handleTap:
method should be executed. If it can find an indexPath
from the users touch, then it returns YES
otherwise it returns NO
.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
UITableView *tableView = (UITableView *)gestureRecognizer.view;
CGPoint p = [gestureRecognizer locationInView:gestureRecognizer.view];
if ([tableView indexPathForRowAtPoint:p]) {
return YES;
}
return NO;
}
一旦我们确定用户是否单击了一个单元格,handleTap:调用方法,然后决定用户是否触摸了图像或单元格的任何其他部分。
Once we have determined if the user has clicked in a cell, the handleTap: method is called, which then decides if the user touched the image, or any other part of the cell.
- (void)handleTap:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableView *tableView = (UITableView *)tap.view;
CGPoint p = [tap locationInView:tap.view];
NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint pointInCell = [tap locationInView:cell];
if (CGRectContainsPoint(cell.imageView.frame, pointInCell)) {
// user tapped image
} else {
// user tapped cell
}
}
}
这篇关于如何区分UITableViewCell的哪个部分已被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!