0

I am using a slide in menu style which loads a UITableView. - ECSlidingViewController

I have about 7 cells in a table view setup as follows:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];

UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];

[cell.contentView addSubview:topSplitterBar];

cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1];
cell.textLabel.shadowOffset = CGSizeMake(0, 1);

UIView *selectedBg = [[UIView alloc] initWithFrame:cell.frame];
selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.selectedBackgroundView = selectedBg;

What would be the best way to show a cell as the selectedBg if that is the currently displayed controller?

I can access the following for example:

if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {

However, I am not sure where would be best practice to set this up? I can do it in the switch case for the cell label setup... For example:

    switch ( indexPath.row ) {
        case 0: {
            if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
                cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
            }
            cell.textLabel.text = NSLocalizedString(@"LMGames", @"Left Menu - Games");
            break ;

However, when a new item is selected from the menu I would need to reload the table each time, is that good? Completing a self.tableView reloadData each time a cell is selected, or is there a better way to approach this?

StuartM
  • 6,743
  • 18
  • 84
  • 160
  • Or just set the `selectedBg` in the `didSelectRowAtIndexPath` method as well. – iwasrobbed May 08 '13 at 01:39
  • I have tried to add this ' UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; ' in the didSelectRowAtIndexPath part but no change happens? – StuartM May 08 '13 at 08:24
  • @iWasRobbed - Ok I got this working with a BOOL being used but only with self tableView reloadData. This was I check in the cell creation if the cell is currentlyDisplayed (as such). The question is, is this the best way to achieve this? If I change the display in didSelectRowAtIndexPath then how do ensure the other cells backgrounds are correct... without reloading the Data of the tableView and checking there. – StuartM May 08 '13 at 12:47
  • Just keep an integer reference to the currently selected row and the previously selected row and then only refresh those rows http://stackoverflow.com/a/5418726/308315 – iwasrobbed May 08 '13 at 13:37
  • @iWasRobbed - thanks please submit as an answer ill accept. Thanks again – StuartM May 08 '13 at 14:53
  • Thanks Stuart! Glad I could be of help to you! – iwasrobbed May 08 '13 at 19:41

1 Answers1

1

Two ideas for you:

  1. Set the selectedBg in the didSelectRowAtIndexPath method in order to set the selected cell.
  2. Keep an integer reference to the currently selected row and the previously selected row and then refresh only those rows by using a method similar to: How to reload and animate just one UITableView cell/row?

I hope that helps!

Community
  • 1
  • 1
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195