NSUserDefault
NSUserDefault

Reputation: 1834

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;
}

Upvotes: 0

Views: 763

Answers (4)

Pratik T
Pratik T

Reputation: 1597

Try this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *objCustomCell = (UITableViewCell *)[tableView  cellForRowAtIndexPath:indexPath];
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NewImage"]];
    objCustomCell.backgroundView = bgView;
}

Upvotes: 1

Rajneesh071
Rajneesh071

Reputation: 31081

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;

}

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

in addition to your code, add the following code.

UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedMenuItem3"]];

cell.selectedBackgroundView = bgView;

For more info, check the documentation for UITableViewCell's selectedBackgroundView

Upvotes: 0

Rushi
Rushi

Reputation: 4500

You can use the following code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView.tag == 3)// We are checking if it's the desire tableview. If you have only one tableview then you can remove this if.
        {
            [tableView reloadData];
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.backgroundView = bgView; // Here set the particular image u want.
        }
    }

Upvotes: 1

Related Questions