user119857
user119857

Reputation: 1771

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.

Upvotes: 17

Views: 27353

Answers (7)

Sayali Shinde
Sayali Shinde

Reputation: 96

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

Upvotes: 1

Hafthor
Hafthor

Reputation: 16896

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

Upvotes: 41

Pragnesh Ambani
Pragnesh Ambani

Reputation: 69

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.

Upvotes: 3

Damien Romito
Damien Romito

Reputation: 10065

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.

Upvotes: 0

Daniel
Daniel

Reputation: 22395

Use the UITableViewCell method

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

Info here.

Upvotes: 0

marcc
marcc

Reputation: 12399

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.

Upvotes: 1

Shaggy Frog
Shaggy Frog

Reputation: 27601

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.

Upvotes: 6

Related Questions