Pip Pip - 3 months ago 29
iOS Question

How to programmatically select a row in a table

I have a table which is viewed by the user. As the user shakes his device, I want a random row to be selected. How do I do this?

I have the shake gesture recognized, I can create an random integer that doesn't exceed the list count, but I can't find the right code to have that row highlighted in the table.

In Apple's documentation I found:

NSIndexPath *rowToSelect; // assume this exists and is set properly
UITableView *myTableView; // assume this exists

[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone;
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNon animate:YES];

but I can't get it working. I have a
UITableView *myTableView
. I have used the randomized integer as the rowToSelect.

Pip Pip

This is how I got it working. I kept the following line:

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:randomRij inSection:0];

I added the following lines:

[eenTabelView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

I commented the following line:

//[[self eenTabelView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];