Archive

Posts Tagged ‘Custom UITableViewCell’

iPhone – UITableViewCell Custom Selection Style Color

January 14th, 2010 Comments off

xcode-iconOddly, Apple has given you the ability to vastly customize the primary view of  a UITableViewCell. However, the selection style view is much more difficult to do such. Apple kindly gave us UITableViewSelectionStyleBlue/Gray/None. Well, that doesn’t always cut it does it?

There are already some very good tutorials on revamping the whole table view.  Google it! The best I came across was from Cocoa with Love – Custom UITableView.

However, this can be a bit overkill for some situations. It can also take a good bit of time to setup all the necessary images. What I needed was a quick and easy way to ‘theme’ the application and thus the selection style for a UITableViewCell. This needed to work with both plain and grouped table view styles and custom cells as well. You can see now why this may take  a while using the above methods.

Here is a quick and easy method to do just that.

SomeViewController.h

#import <UIKit/UIKit.h>

@interface SomeViewController : UITableViewController {
     UITableViewCell *currentSelectedCell;
}

@property (nonatomic, retain) UITableViewCell *currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell
     deselectedCell:(UITableViewCell *)deselectedCell;

@end

SomeViewController.m

#import "SomeViewController.h"

@implementation SomeViewController

@synthesize currentSelectedCell;

- (void)setSelectedCell:(UITableViewCell *)selectedCell deselectedCell:(UITableViewCell *)deselectedCell {
     //revert deselected cell
     if (deselectedCell != nil) {
          deselectedCell.backgroundColor = [UIColor whiteColor];
          for (UIView *subView in [deselectedCell.contentView subviews]) {
               if ([subView isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)subView;
                    label.textColor = [UIColor blackColor];
               }

               if ([subView isKindOfClass:[UITextField class]]) {
                    UITextField *textField = (UITextField *)subView;
                    textField.textColor = [UIColor blackColor];
               }
          }
     }

     //setup selected cell
     selectedCell.backgroundColor = [UIColor redColor]; //Some defined UIColor
     for (UIView *subView in [selectedCell.contentView subviews]) {
          if ([subView isKindOfClass:[UILabel class]]) {
               UILabel *label = (UILabel *)subView;
               label.textColor = [UIColor whiteColor];
          }

          if ([subView isKindOfClass:[UITextField class]]) {
               UITextField *textField = (UITextField *)subView;
               textField.textColor = [UIColor whiteColor];
          }
     }
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
     (NSIndexPath *)indexPath {

     setSelectedCell:[tableView cellForRowAtIndexPath:indexPath]
          deselectedCell:currentSelectedCell];

     currentSelectedCell = [tableView cellForRowAtIndexPath:indexPath];
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     //........Setup UITableViewCell...........//

     cell.selectionStyle = UITableViewCellSelectionStyleNone;

     return cell;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:
     (NSInteger)section {

     return count;
}

@end

Now, I’ve provided this in one class but what I’ve done is setup an ‘application’ singleton that has this function and is called from each UITableView’s didSelectRowAtIndexPath delegate. Works great on standard, custom, plain, and grouped views.