Question

-Hi, I have a problem with a tableview reloading data and what I want to do is that you put the selected cell in blue and change an image that is, the problem is that if I make the [self.mytableview reloadData] in the didselectedRow blue background disappears and if I do the image of the cell does not change, I'm a bit lost with this piece of code I give thanks

if (indexPath.row == _selectedRow) {

            UIImageView *favView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"favIconSelected.png"]];
            CGRect frame = favView.frame;
            frame.origin.x = 294;
            frame.origin.y = 7;
            favView.frame = frame;

            [cell.contentView addSubview:favView];

            [favView release];
        }

 cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:SELCETED_BGIMGCELL]]autorelease];



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


    Product *p = [_productList objectAtIndex:indexPath.row];

    _selectedRow=indexPath.row;
    [_delegate productWasSelected:p];
    [self.myTableView reloadData];

}
Was it helpful?

Solution

You don't need to call reloadData at all. All you need to do is to update the cell that was selected.

In the didSelectRowAtIndexPath method you can get the cell directly by calling cellForRowAtIndexPath on the tableView. This will return the actual cell that's on display and you can directly set the image it's displaying.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = ...;

Alternatively you could control the image display from your cell subclass if you provide it with both the normal and selected images when you configure each instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top