i got a UITableViewCell which i want to stay selected when clicked on it. So i have this code which works just fine:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [[[self.dataSource objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension]];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor brownColor]];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
Everytime i click on a cell it gets selected with the brown color and stays selected until i click another cell… perfect
But when i dismiss the view by clicking on the back button of my navigationcontroller and then i switch back to my view my cell is not selected anymore. So how can i achieve that the cell which was selected before i switched the views still is selected when i come back to the view ?
I thought i maybe have to create a property from the tableview and then select the row in the viewDidLoad again. The selectedRow index i could save in the nsuserdefaults.. but i hope there is a simpler solution.
In your
-viewDidLoadmethod, add this to the bottom:self.clearsSelectionOnViewWillAppear = NO;Which would make it look something like this: