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

- 16,351
- 19
- 115
- 215

- 1,771
- 3
- 12
- 9
-
See this question: http://stackoverflow.com/questions/19295297/uitableviewcell-set-selected-initially/25128427#25128427 – Drew C Jun 14 '15 at 04:56
7 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.
-
11. this does not work for me. 2. Apple says you should use checkmark to indicate an item's selection state. – Fabien Warniez Apr 26 '14 at 06:04
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
.

- 27,575
- 16
- 91
- 128
-
I am trying to replicate the UI Apple use when you add/edit an event in the Calendar application and set a start and end date/time. I saw comments about the checkmark rule elsewhere, but am uncertain whether it would apply in this case (the table is used to layout components rather than present a list of data). – user119857 Aug 24 '09 at 19:55
-
That's an interesting point. In this case, they don't need to have a subview for each the Starts and Ends since they are both representing the same kind of data, modified by the same kind of UIPickerView. So that would represent a judicious use. They are likely using UITableView's selectRowAtIndexPath:animated:scrollPosition: method... see http://tinyurl.com/yd9zs65 – Shaggy Frog Oct 07 '09 at 19:38
You should put it in viewWillAppear
.
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:0];
If you try to select it in cellForRowAtIndexPath:
, then it will not take the required style.

- 6,014
- 5
- 44
- 74

- 69
- 3
Definitely watch out. I'm sure you have a good reason, but look closely at the Human Interface Guidelines document Apple provides. Apps get rejected for not unselecting table rows. I'd encourage you to find the appropriate section of the HIG and see Apple offers any suggestions.

- 12,295
- 7
- 49
- 59
Use this code to select cell by default in table view, indexPath
can be vary according to your need
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}

- 6,052
- 10
- 43
- 117

- 96
- 3
Use the UITableViewCell method
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
Info here.

- 22,363
- 9
- 64
- 71
-
Maybe following method suits better? http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableViewCell/setHighlighted:animated: – Harri Siirak Aug 24 '09 at 17:10
-
-
1O i f ound it, that one highlights it and does not select it, I guess if thats all the asker wants then sure thats more suitable – Daniel Aug 24 '09 at 17:28
Sometimes, when you're not in a UIViewController
, you have no viewWillAppear
, and sometimes, you created your UITableView
programmatically.
The easy solution is to implement this delegate
method:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndex == indexPath.row) {
cell.selected = YES;
}
}
It's does not work in the cellForRowAtIndexPath
because the cell is not yet displayed. and the setSelected
method is called just when this one is displayed.

- 6,014
- 5
- 44
- 74

- 9,801
- 13
- 66
- 84