17

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

1

7 Answers 7

41
-(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.

1
  • 1
    1. this does not work for me. 2. Apple says you should use checkmark to indicate an item's selection state. Commented Apr 26, 2014 at 6:04
6

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.

2
  • 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
    Commented Aug 24, 2009 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 tinyurl.com/yd9zs65 Commented Oct 7, 2009 at 19:38
3

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.

1

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.

1

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];
}
0

Use the UITableViewCell method

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

Info here.

3
  • Maybe following method suits better? developer.apple.com/iphone/library/documentation/UIKit/…: Commented Aug 24, 2009 at 17:10
  • which method? the link doesnt go to any particular method
    – Daniel
    Commented Aug 24, 2009 at 17:27
  • 1
    O 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
    Commented Aug 24, 2009 at 17:28
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.