KDM
KDM

Reputation: 197

Setting Image in custom UITableviewCell does not *Stick*

I have a custom UITabvleViewCell that has a UIImageView in it. I can set the image fine when the cell is setup in cellForRowAtIndexPath (though I don't) however under certain conditions I need to change the image and have been doing so with the following code.

-(void)updateCellForUPC
{
    AppData* theData = [self theAppData];

    NSInteger cellIndex;
    for (cellIndex = 0; cellIndex < [productArray count]; cellIndex++) {

      NSString *cellUPC = [NSString stringWithFormat:@"%@",[[productArray objectAtIndex:cellIndex] objectForKey:@"UPC"]];

        if ([cellUPC isEqualToString:theData.UPC]) {

            OrderDetailCell *activeCell = [[OrderDetailCell alloc] init];
            activeCell = (OrderDetailCell *) [orderDetailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:0]];

            UIImage* image = [UIImage imageNamed:@"checkmark.png"];
            activeCell.statusImageView.image = image;
            activeCell.checked = YES;
        }
    }
}

This works and the image gets updated however when you scroll the cell off the screen and back onto the screen the image is reset!

I need it to *stick with the new image.

Upvotes: 0

Views: 117

Answers (2)

KDM
KDM

Reputation: 197

For anyone interested in how I did this in the future.

I made a NSMutableArray and I added the index as a NSString.. I also set the image for the cell if it's being displayed at the time.

-(void)updateCellForUPC
{
    AppData* theData = [self theAppData];

    NSInteger cellIndex;
    for (cellIndex = 0; cellIndex < [productArray count]; cellIndex++) {

      NSString *cellUPC = [NSString stringWithFormat:@"%@",[[productArray objectAtIndex:cellIndex] objectForKey:@"UPC"]];

        if ([cellUPC isEqualToString:theData.UPC]) {

            OrderDetailCell *activeCell = [[OrderDetailCell alloc] init];
            activeCell = (OrderDetailCell *) [orderDetailTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:0]];

            UIImage* image = [UIImage imageNamed:@"checkmark.png"];
            activeCell.statusImageView.image = image;

            NSString *index = [NSString stringWithFormat:@"%d",cellIndex];
            [selectionArray addObject:index];
        }
    }
}

Then in my cellForRowAtIndexPath I just use the following code to check if the index is in the array and set my image if it is. That way when the cell is redrawn (scrolled) it has the image.

NSString *index = [NSString stringWithFormat:@"%d",indexPath.row];

if ([selectionArray containsObject:index]) {
    UIImage* image = [UIImage imageNamed:@"checkmark.png"];
    cell.statusImageView.image = image;
}

Upvotes: 0

Stavash
Stavash

Reputation: 14304

This is not recommended as you are not gaining the benefit of the dequeue mechanism for unused cells. Consider doing this when you create the cell, or create a mechanism that knows which cells this should be done to.

Upvotes: 1

Related Questions