I want to exclude subview UITableViewCell due to background change when I selected it

I want to exclude UITableViewCell ( viewz ) from changing the background when I select it.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleBlue; UIView *viewz = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; viewz.backgroundColor = [UIColor redColor]; [cell.contentView addSubview:viewz]; 

No cell selected. Everything is good.

http://img32.imageshack.us/img32/1599/screenshot20121129at123.png

The cell changed color to blue. Everything is good. But I do not want my viewz to change the background color to blue. How can i do this?

enter image description here

+4
source share
3 answers

Add an empty implementation of the setSelected: animated: method to your subclass of UITableViewCell

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { } 
+6
source

It seems you need to touch the background color, or the default cell implementation will change it for you. If you add a colored border, you will see that viewz still exists, even if you comment out the line where I change the background color:

 #define VIEWZ_TAG 1234 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... UIView *viewz = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; viewz.backgroundColor = [UIColor redColor]; viewz.tag = VIEWZ_TAG; viewz.layer.borderWidth = 1; viewz.layer.borderColor = [UIColor whiteColor].CGColor; [cell.contentView addSubview:viewz]; ... } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isSelected]) { UIView *viewz = [cell viewWithTag:VIEWZ_TAG]; viewz.backgroundColor = [UIColor greenColor]; } } 

If you want to precisely control a cell when it is selected, you can use a custom UITableViewCell .

+2
source

You add only a custom cell and in the selection bar, you change the background color of this view.

0
source

All Articles