我怎么能区分UITableViewCell的哪个部分被点击

我用自定义的UITableViewCell创build了一个UITableViewCell 。 我的单元格包含左侧的一个UIImageView和右侧的UITextView

UITableViewController里面,我在tableview cellForRowAtIndexPath设置了图像和文本。

一切都显示正常,但现在我需要实现didSelectRowAtIndex ,我需要区分单元格的UIImageViewUITextView已被点击。

比方说,点击图像代表删除操作和其他单元格编辑操作。

您可以将一个手势识别器添加到每个单独的单元格中,而不是将其添加到表格视图中,并确定从用户触摸点select哪个单元格,然后确定用户是否触摸了图像或单元格。

首先确保你的控制器采用UIGestureRecognizerDelegate协议。

 @interface MyTableViewController() <UIGestureRecognizerDelegate> @end 

然后在加载视图时将UIGestureRecognizer添加到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:方法。 如果可以从用户触摸中findindexPath ,则返回YES否则返回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:方法,然后决定用户是否触摸了图像或单元格的任何其他部分。

 - (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 } } } 

你可以touchesEnded UITableViewCell并覆盖touchesEnded

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; UIView *hitView = [self hitTest:location withEvent:event]; if (hitView == myImageView) ...; if (hitView == myTextView) ...; } 

你需要保留一些对你的UIImageViewUITextView引用(它们应该是你的单元格的属性)。

你当然可以覆盖touchesBegan而不是touchesEnded ,取决于你想要达到的function。

一个非常抽象和一般的答案是做下面的每个UIImage和UILabel你添加设置他们的标签是indexPath.row

 //When creating the label and image add a recognizer to them label.tag = indexPath.row; imageView.tag = indexPath.row; 

然后在每个图像和标签上添加一个UITapGestureRecognizer,就像这样

  UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [label addGestureRecognizer:recognizer]; [imageView addGestureRecognizer:recognizer]; } - (void) handleTap:(UITapGestureRecognizer*)recognizer { UIView *view = recognizer.view; int row = view.tag; if ([view isKindOfClass:[UILabel class]]) { //Row is row //and the label is pressed } if ([view isKindOfClass:[UIImageView class]]) { //Row is row //and the imageview is pressed } } 

使图像成为UIButton。 当button的动作被触发,你知道用户点击图像(单元格将不被选中)。 如果选中单元格,则知道用户在该行的其他位置(包括文本视图或标签)轻敲了一下。

此外,设置button的标签属性,例如单元格的行索引,以便您可以知道哪一行的图像被点击。

你有两个select是实现: – 1 – 在你的“uitableviewcell”中添加UITapGestureRecognizer,并使其指向图像视图,并将“indexpath”作为parameter passing给select器,并使委托传递给tableviewcell

 UILabel *label = =[UILabel alloc]init]; label.userInteractionEnabled = YES; UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)] autorelease]; [label addGestureRecognizer:tapGesture]; -(void)labelTap{ [delegate performselector@selector(labeltapped:)withobject:indexpath]; } 

第二种方法是检查[imageview或label]types的DidSelectRowAtIndexPath的发送者; 但我更喜欢第一种方式