I have a custom UITableViewCell loaded from a nib. In it are three UIImageView views. In the -(UITableViewCell*) tableView:(UITableView*) cellForRowAtIndexPath:(NSIndexPath*)indexPath method I check a property for each row and determine whether each one is visible. If they are not visible, I shift the x position of the image so that there are no empty spaces. I am using:
cell.imageView1.hidden = YES;
cell.imageView2.hidden = YES;
cell.imageView3.hidden = YES;
int x = 0;
CGRect frame1 = cell.imageView1.frame;
if (property1)
{
cell.imageView1.hidden = NO;
frame1.origin.x = x;
x += SPACING;
}
CGRect frame2 = cell.imageView2.frame;
if (property2)
{
cell.imageView2.hidden = NO;
frame2.origin.x = x;
x += SPACING;
}
// etc...
For some reason, when the table is initially shown, the images are in the wrong location, but if I scroll up and down so that the cell is not shown then shown again, the image location goes to its correct place. What is causing this?
Thanks to Felipe for pointing me to the right solution. In my UITableViewCell subclass I overrode the layoutSubviews method to properly layout the images there according to their state.