iOS - when to remove subviews from UITableViewCell

Posted by Milad Ghattavi on Stack Overflow See other posts from Stack Overflow or by Milad Ghattavi
Published on 2012-07-06T09:01:44Z Indexed on 2012/07/06 9:15 UTC
Read the original article Hit count: 289

Filed under:
|
|

I have a UITableView which a UIImage is added as a subview to each cell of it. The images are PNG transparent. The problem is when I scroll through the UITableView, the images get overlapped and then I receive the memory warning and stuff.

here's the current code for configuring a cell:

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

    int cellNumber = indexPath.row + 1;
    NSString *cellImage1 = [NSString stringWithFormat:@"c%i.png", cellNumber];
    UIImage *theImage = [UIImage imageNamed:cellImage1];
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
    [cell.contentView addSubview:cellImage];

    return cell;
}

I know the code for removing the UIImage subview would be like:

[cell.imageView removeFromSuperview];

But I don't know where to put it. I've placed it between all the lines; even added an else, in the if statement. didn't seem to work!

© Stack Overflow or respective owner

Related posts about uitableviewcell

Related posts about uiimage