I have created a UITableView
and would like a specific UITableViewCell
to appear selected (blue) when the view is loaded.
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.