How can I set a row selected by default? I want to select a row , take the number of selected row with indexpath.row
and then reload the table and select the ro
NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
will give you the NSIndexPath
for the current selected row. You can then do
[tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionMiddle];
to select that row (and show it in the middle of the screen) later.
This is how you preselect a row in a UITableView:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Set the indexPath to be the row you want selected and then make the method call as above.
This will do it:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Where row is the cell number you want to select (starts 0...), and section is the section where the cell appears.
I had the same question, and this is how I did it:
In your tableViewController
, add this in this function
(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 'the row you want to select')
{
[tableView
selectRowAtIndexPath:indexPath
animated:TRUE
scrollPosition:UITableViewScrollPositionNone
];
[[tableView delegate]
tableView:tableView
didSelectRowAtIndexPath:indexPath
];
}
}
Use the following code to get the selected rows in a table view. :)
NSArray *selectedRows=[tableView indexPathsForSelectedRows];
NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {
NSIndexPath *indexPath = [selectedRows objectAtIndex:i];
NSInteger row = indexPath.row;
NSNumber *number = [NSNumber numberWithInteger:row];
[rownumberArray addObject:number];
}
// rownumberArray contains the row numbers of selected cells.
I realize that this question is rather old (2011), but just to also post an up-to-date answer, in case someone (like me) searches for this problem and stumbles across this question:
With iOS 9, Apple has improved UITableView
a lot. To make sure that focus is not lost when a table is reloaded, just do this:
tableView.remembersLastFocusedIndexPath = Yes;
and it will "magically work".
Even better, implement
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView
in the UITableViewDelegate
to let the table know which row you want selected when it is drawn to screen for the very first time.
Before iOS 9, I'd usually just write my own reloadData
method, that gets the currently selected row, reloads the table and then selects it again. Either in the UITableViewController
or if I needed that to work with a table where I couldn't prevent code calling reloadData
directly, I subclassed UITableView and overrode reloadData
(hey, delegation is the preferred way, but sometimes you just have to subclass and override, even in iOS).