Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force UITableViewCell selection using selectRowAtIndexPath?

I am trying to force selection(highlight) of a UITableViewCell using selectRowAtIndexPath..

For eg, Lets say i want to always have the first cell in a table view to be highlighted,I apply the following logic in viewDidAppear

[self.tableViewPrompts selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

Works nicely.But when i try to apply the same logic elsewhere in the application the cell is not selected.I even tried deselecting other cells before calling the above method and reloading tableview before and after calling this method but none of the approaches seem to work!

I even used the tableview's delegate to forcibly make a call to the didSelectRowAtIndexPath.Event this does not work.

PS: The selection of the cell does not trigger any action all i am trying to achieve is highlighting the desired cell.

What am i doing wrong here?

Help is greatly appreciated!!

like image 312
Mr.Anonymous Avatar asked Feb 26 '26 02:02

Mr.Anonymous


1 Answers

The selection of the cell does not trigger any action all i am trying to achieve is highlighting the desired cell.

Why don't you use the cell's highlighted property?

UITableViewCell *cell = [self cellForRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0]];
cell.highlighted = YES;

Cant test it now, but it should work.....

edit

Better yet, of course: change your cellForRowAtIndexPath to do that

-(UITableViewCell) tableView:cellForRowAtIndexPath:
//...
if (indexPath.row == 0 && indexPath.section == 0) cell.highlighted = YES
else cell.highlighted = NO;
//...
return cell;
like image 92
Mario Avatar answered Feb 28 '26 16:02

Mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!