Search code examples
iphoneiosuitableviewuibackgroundcolor

UItableviewcell background color while click


How to set a color to the entire cell which selected or checkmarked and reset to previous color while in unchecked in uitableview?I tried but it showing a background color for a sec.can any one please help me to code.


Solution

  • Ok, here I think you want to color your cell when selected and another color if unselected,leaving those selected cells with the previous color. So have an array states globally. In didSelectRowAtIndexPath :

    if (![myMutableArray containsObject:indexPath]) {
        [myMutableArray addObject:indexPath]; 
    }
    else{
        [myMutableArray removeObject:indexPath];
    }
    
    [sampleTableView reloadData];
    

    And in cellForRowAtIndexPath :

    if ([myMutableArray containsObject:indexPath]) {
        cell.backgroundColor = [UIColor redColor];
    }
    else{
        cell.backgroundColor = [UIColor greenColor];
    }