Hi All, I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me
Thanks in Advance Shibin
Hi All, I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me
Thanks in Advance Shibin
I do not think you can use a custom color. However, you can use the following property of UITableViewCell
@property(nonatomic) UITableViewCellSelectionStyle selectionStyle
The selection style is a backgroundView constant that determines the color of a cell when it is selected. The default value is UITableViewCellSelectionStyleBlue. Since
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
you can switch from the default blue to gray, or no colored selection at all.
Another way to do it would be to move in a new view over your cell, with whatever color you'd like and 50% or so opacity. You move this view over to the cell when you get a -setSelected:animated: call. When I say move, you actually could always have a view on top of your cell, but just turn the hidden bit off and on as you need.
The best way to do this is like this:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
}
// Set up the cell...
cell.textLabel.text = @"foo";
return cell;
}
The relevant part for you is the cell.selectedBackgroundView = v;
instruction.
You can substitute the very basic view 'v' here with any view you like.
!!! OMG THANK YOU
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
is the answer!!!
!!!
Very good answer, do you know how to stop buttons on a custom cell being selected?