How to programmatically "tap" a UITableView cell?

24,842

Solution 1

Can't you put any logic in didSelectRowAtIndexPath into a separate method and just call that method from both didSelectRowAtIndexPath and wherever else you want to call the same code?

Solution 2

if you want to have the cell selected, i.e highlight a specific cell:

//select first row of first section
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:selectedCellIndexPath animated:false scrollPosition:UITableViewScrollPositionMiddle];

if you want to additionally trigger actions in your didSelectRowAtIndexPath method, you need to manually call the delegate method, it won't happen automatically:

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

Solution 3

Swift 3.2 Solution

let indexPath = IndexPath(row: 2, section: 0)  // change row and section according to you
tblView.selectRow(at: indexPath, animated: true)
tblView.delegate?.tableView!(tblView, didSelectRowAt: indexPath)

Solution 4

It's just a method. Go ahead and invoke it like you'd invoke any other method.

Solution 5

This is a 6.1 update. When you are using segues- all the advice is good, but you also have to invoke the segue. So - to add to the advice and summarize it

// ... assuming we just added a new row - which is one use of what this thread is trying to do

/* get new count - this is the row we are going to highlight - 0 based */
 int newIndex = [dataArrayUnderlyingTable count]-1; 

/* make it look pretty by highlighting it */
    NSIndexPath *nip = [NSIndexPath indexPathForRow:newIndex inSection:0];
    [[self tableView] selectRowAtIndexPath:nip animated:YES scrollPosition:UITableViewScrollPositionBottom];

/* run the code in the following method so it is not missed */
    [self tableView:[self tableView] didSelectRowAtIndexPath:nip];

/* now 'tap' on it ... or simulate it */
    [self performSegueWithIdentifier: @"viewChildDetails" sender: self];
Share:
24,842
Christian Gossain
Author by

Christian Gossain

Updated on July 27, 2020

Comments

  • Christian Gossain
    Christian Gossain almost 4 years

    I was wondering is there a way that I could have my code "tap" a cell in my UITableView in order to reproduce the behaviour specified in the - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method.

    I guess in other words, is is possible to invoke the - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method programatically?

  • mostafa tourad
    mostafa tourad about 11 years
    That actually is an instance method, not a class method - but hey, that might be nitpicking :D.
  • Ethan Parker
    Ethan Parker over 9 years
    Yes but the cell doesn't get highlighted like it does when you tap. Any ideas there?
  • pronebird
    pronebird over 9 years
    Calling didSelectRowAtIndexPath does not trigger segues though.
  • Robert J. Clegg
    Robert J. Clegg about 9 years
    Yes it does. Works for me.
  • Rob
    Rob over 8 years
    @BeemerFan: Just call from the method that calls the new, common method: [self.tableView selectRowAtIndexPath:myIndexPath animated:false scrollPosition:UITableViewScrollPositionNone];
  • Nike Kov
    Nike Kov over 6 years
    Seems like it was taken here stackoverflow.com/a/40590050/5790492 !