Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a UITableViewCell by default?

I have created a UITableView and would like a specific UITableViewCell to appear selected (blue) when the view is loaded.

like image 764
user119857 Avatar asked Aug 24 '09 16:08

user119857


2 Answers

-(void)viewWillAppear:(BOOL)animated {
    // assuming you had the table view wired to IBOutlet myTableView
    // and that you wanted to select the first item in the first section
    [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                             animated:NO 
                       scrollPosition:UITableViewScrollPositionTop];
}

I'm using this technique to select one of two UITableViewCells so the users knows which cell a UIDatePicker below the table view it will effect. This technique is used by Apple in the calendar app when you create a new event and set the dates.

like image 187
Hafthor Avatar answered Sep 28 '22 17:09

Hafthor


Be judicious using this method, as selecting the row in this way is something Apple suggests against doing to show a "chosen" state.

Instead, consider setting the cell's accessoryType property to something like UITableViewCellAccessoryCheckmark.

like image 32
Shaggy Frog Avatar answered Sep 28 '22 16:09

Shaggy Frog