I am developing an iPhone application, in my table view I wanted custom color for Cell Selection Style, I read the UITableViewCell Class Reference but there are only three constants defined for Selection style (Blue, Gray, None). I saw one application that used a different color than those defined in the reference.
How can we use a color other than those defined in the reference?
Thanks in advance.
The best way to set the selection is to set the selectedBackgroundView
on the cell when you construct it.
i.e.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground.png"]] autorelease]; } // configure the cell }
The image used should have a nice gradient (like the default selection). If you just want a flat color, you can use a UIView instead of a UIImageView
and set the backgroundColor
to the color you want.
This background is then automatically applied when the row is selected.
Setting the selectedBackgroundView
seems to have no effect when the cell.selectionStyle
is set to UITableViewCellSelectionStyleNone
. When I don't set the style is just uses the default gray.
Using the first suggestion that inserts the custom UIView
into the cell does manipulate the cell but it doesn't show up when the cell is touched, only after the selected action is completed which is too late because I'm pushing to a new view. How do I get the selected view in the cell to display before the beginning of the selected operation?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With