views:

5661

answers:

3

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.

+1  A: 

Override didSelectRowAtIndexPath: and draw a UIView of a color of your choosing and insert it behind the UILabel inside the cell. I would do it something like this:

UIView* selectedView; //inside your header

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

  UITableViewCell* cell = [tableView cellAtIndexPath:indexPath];
  selectedView = [[UIView alloc] initWithFrame:[cell frame]];
  selectedView.backgroundColor = [UIColor greenColor]; //whatever

  [cell insertSubview:selectedView atIndex:0]; //tweak this as necessary
  [selectedView release]; //clean up

}

You can choose to animate this view out when it gets deselected and will satisfy your requirements.

Jeffrey Forbes
This approach avoids Apple's built-in selectedBackgroundView on every UITableViewCell. If you use the selectedBackgroundView, your selection will animate in and out and will also unselected when instructed.
Matt Gallagher
It would be better to override `setSelected:highlighted:` in the cell and do the logic there if you are going to take this approach so it works in all view controllers. Like Matt said, using `selectedBackgroundView` is a much better approach.
Sam Soffes
+18  A: 

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.

Matt Gallagher
What about for grouped style table views? I just used a rectangle image and it showed through the rounded curves of the tableview cells.+1 for a great answer.
JoePasq
Matt Gallagher
Can you not do this without using images?
ing0
@james.ingham: you could use any kind of view for the selectedBackgroundView. You could use a regular UIView and set a flat backgroundColor. You could use a custom drawn UIView subclass.You cannot simply set a different tint; this can't be done.
Matt Gallagher
should do an autorelease on the selectedBackgroundView so it's not leaking.
stigi
This is the extact solution i was looking for ....Great answer.
Biranchi
+2  A: 

Setting the selectedBackgroundView seems to have no effect when the cell.selectionStyle is set to None. When I don't set the style is just uses the default gray.

Using the first suggestion that inserts the custom IView 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?

Paul