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 .
source share