Jon
Jon

Reputation: 4732

Set first cell to highlighted on view's load

I have a UISplitViewController. When the app launches initially, the detail view is showing the detail data for the first row. However, the cell in the table is not highlighted, since it hasn't been selected yet. How can I set it to selected by default when the app loads initially?

I added this to cellForRowAtIndexPath but its not highlighting. It highlights fine when I actually select a cell.

if (indexPath.row == 0)
{
    cell.selected = YES;
}

Upvotes: 2

Views: 265

Answers (1)

chown
chown

Reputation: 52778

-(void)selectFirstRow {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    // optional:
    // [self tableView:myTable willSelectRowAtIndexPath:indexPath];
    [myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    // optional:
    // [self tableView:myTable didSelectRowAtIndexPath:indexPath];
}

Then call [self selectFirstRow]; wherever you need to.

Upvotes: 1

Related Questions