NSUserDefault NSUserDefault - 4 months ago 6
iOS Question

How to remove the background image from a UITableViewCell?

In my app I am having

UITableView
in one of my
UIView
.I added a background image for all cells. My problem is , when I select a particular cell,I want to change the background image of that particular cell alone and all the other cells should have a old background image.
How can i do this. Please share your ideas.

Here is my
cellForRowAtIndexPath
code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MenuItem3"]];

cell.backgroundView = bgView;



cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];



cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [arrayValue objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Answer

No Need to reload all your table just reload PreviousInxed like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }


    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bck.png"]];

    UITableViewCell *celld = (UITableViewCell *)[tableView cellForRowAtIndexPath:table.indexPathForSelectedRow];
    celld.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];

    return cell;
}

NSIndexPath *perviouseIndexPathaf;

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

    if (perviouseIndexPathaf)
    {
        [table reloadRowsAtIndexPaths:[NSArray arrayWithObject:perviouseIndexPathaf]
                     withRowAnimation:UITableViewRowAnimationNone];
    }
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];
    perviouseIndexPathaf = indexPath;

}