Nate
Nate

Reputation: 530

Background color change on table view cell tap in IOS

I have a view controller that has a UITableViewController as its main view. In the function - tableView:cellForRowAtIndexPath: I create a custom view for my cell with the following code.

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                        CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:CellIdentifier];
}


cell.backgroundView = [[UIView alloc] init];

I then add my own custom views to cell.backgroundView. However, when I tap a cell the cell becomes completely gray. No where in my code do I specify that this should happen. To do some testing I added some text to the standard part of a cell as follows.

cell.textLabel.text = @"TEST";

When I ran this I saw both my custom view and the 'TEST' label. When a cell was pressed, the gray screen appeared and this time 'TEST' was visible and my custom view still not visible. My guess is that some other measure has to be taken to ensure custom views are visible. I was using the tableView:didSelectRowAtIndexPath: function to register taps but oddly this function is called for all tap events except the first one. In addition, I tried to set the background color here but it didn't work.

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.backgroundColor = [UIColor whiteColor];

}

Any help is appreciated.

Upvotes: 3

Views: 5248

Answers (5)

oskarko
oskarko

Reputation: 4178

Swift,

Set up cell.selectionStyle = .none in your cellForRowAt method from the UITableViewDataSource.

Upvotes: 0

Sudipto Roy
Sudipto Roy

Reputation: 6795

One of the best way to keep background color unchanged is:

  • Set YOUR_VIEW.backgroundColor to clearColor
  • Then set YOUR_VIEW.layer.backgroundColor to YOUR_COLOR

Let's see an Objective-C implementation:

self.YOUR_VIEW.backgroundColor = UIColor.clearColor;
self.YOUR_VIEW.layer.backgroundColor = UIColor.YOUR_COLOR.CGColor; 

Note : It doesn't need to turn off beautiful selection or highlight animation.

Upvotes: 0

Louie
Louie

Reputation: 5940

The accepted answer was not working for me. This problem driving me crazy for awhile because it would look like a "glitch" on any selection when the next view loaded..

My SOLUTION: I had to go to the .xib file of the actual TableViewCell (my custom cell that I built) select the cell and select the CellView. (Make sure you select the Cell, and NOT a subview within it. It will turn Blue like the picture shows). On the left side click "Selection" -> "None". This SHOULD be the exact same as doing it with code, but for some reason it just wasn't working...

enter image description here

Upvotes: 0

AdamPro13
AdamPro13

Reputation: 7400

In tableView:cellForRowAtIndexPath: change the selectionStyle property on each cell to UITableViewCellSelectionStyleNone.

Upvotes: 7

Nagarajan Sathish
Nagarajan Sathish

Reputation: 137

You can do the same in storyboard by setting the Selection property of the Table View Cell to None

Upvotes: 0

Related Questions