How to identify the Touch event on the UITableView and get the cell index from the table (IPhone SDK) for this event?

What you need, you just need to get a Touch event on the UITableView and then get the cell index from the table for this event. Just want to say that I don’t consider using custom methods for custom cells!

    3 answers 3

    Wow, well, you guys made a whole battle here :). In fact, the answer is very simple: it follows from the delegate method, which is called -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{} . We click on the visible cells and get the desired index - everything is correct, at the entrance to the further execution of the task there was a need to add a picture to each cell, falling into which deletion takes place. I wanted to create a small area in the size of the picture in which the touch would be recognized and the action would take place, but I was slightly mistaken - as a result I found a simple solution. On each cell I create a button, but I do not attach a tag , but I do the following:

     [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 

    Well, actually the method or selector, as you like:

     (void)checkButtonTapped:(id)sender event:(id)event { NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:SimpleTable]; red = [SimpleTable indexPathForRowAtPoint: currentTouchPosition]; if (red != nil) { rocket = red.row; NSLog(@"index = %@ = %i", red, rocket); } } 

    Thanks to all!

      You will have to create a custom UITableView in which you must define methods.

      Responding to Touch Events

       – touchesBegan:withEvent: – touchesMoved:withEvent: – touchesEnded:withEvent: – touchesCancelled:withEvent: 

      Read more here: UIResponder Class Reference

      • This is a huge crutch, even if it’s all right, it’s better to use UIGectureRecognizer - AlexDenisov
      • Well, your decision is not the answer to the question. A person wants to get a touch event. And just find the selected cell can anyone. UIGestrureRecognizer works only on iOS> = 4.0, which inserts even more crutches than to catch touch-events in the custom table. Therefore, your -1 is rather strange. - Nekto
      • Well, firstly, not a word was said about the platform, and secondly, the definition of a custom table is an even stranger solution. In addition, the questioner may not need to completely catch the touch, but just need to catch a pointer to a table cell. - AlexDenisov
      • Okay, you can't understand the question on SO, because they write in English there. But everything is written here clearly - Touch-cобытие . Moreover, if you are not sure about something, then it is all the more strange that you immediately zaminusovali without understanding the essence of the issue. - Nekto
      • I am sure that in this context, your solution is a crutch; instead of implementing a custom table, you can use a solution that I provided as a second option. Let's not be holivarit, if I offended you badly with a minus, then I'm sorry. - AlexDenisov

      First, you specify your controller class as your delegate to the table, via InterfaceBuilder, or from code

       tableView.delegate = self; 

      and in your controller class implement the method

       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%d", indexPath.row); // вот в вашем indexPath будет информация об индексе ячейки и об индексе секции UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // непосредственно ваша ячейка } 

      Option two, if you need it through the touch event

      Create a UITapGestureRecognizer in the controller and set a method for it that will be called upon tapes; this method must be defined in the class that you pass as the target, in our case it is self

       UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableTapped:)]; tapRecognizer.numberOfTouchesRequired = 1; [self.tableView addGestureRecognizer:tapRecognizer]; [tapRecognizer release]; 

      And in the tapa handler, you already calculate the desired cell.

       - (void)tableTapped:(id)sender{ UITapGestureRecognizer *rec = (UITapGestureRecognizer *)sender; CGPoint tapPoint = [rec locationInView:self.tableView]; NSIndexPath *path = [self.tableView indexPathForRowAtPoint:tapPoint]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; }