1

This problem has had me stuck for a while now and I can't for the life of me see why it is occurring.

I have a tableView with custom cells inside it. The cell has an imageView top left and then various information which is looked up from a plist. I am using auto-layout which sets the position of all the elements. I have refreshed all the constraints so they are all updated. See below:

enter image description here

The problem occurs when I scroll the view down and the cell goes out of screen. I am dequeing them so each time they will be refreshed.

BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier];

When they do refresh though the constraints they were bound by now don't work. The imageview is 60x60 pixels and the image is 120x120 pixels and named fruitMainImage@2x.png to take retina screens into account.

enter image description here

I have put a red border around the picture to make it easier to see what is happening. As you can see the image is moving down for some reason once refreshed.

I have tried switching to frame layout to fix it in position and this doesn't work.

I found this stackoverflow question which seems to raise the same issue but only raises it as an issue instead of helping get round the issue in anyway.

Any help would be greatly appreciated as I don't fancy rewriting all my code with keeplayout if there is an easy way to fix it.

EDIT:

Here is my cellForRowAtIndex method:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier];

    NSDictionary * dict = [_settingsData objectForKey:_settingsPacks[indexPath.row]];

    cell.nameLabel.text = [dict objectForKey:bSettingsName];
    cell.byLabel.text = [dict objectForKey:bArtist];
    cell.stickerImageView.image = [UIImage imageNamed:[dict objectForKey:bImage]];

    // To make it obvious that the view is changing position
    cell.stickerImageView.layer.borderColor = [UIColor redColor].CGColor;
    cell.stickerImageView.layer.borderWidth = 2;

    cell.descriptionView.text = [dict objectForKey:bDescription];

    [cell.linkButton setTitle: [dict objectForKey:bLink] forState: UIControlStateNormal];
    cell.linkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

and also a shot of the auto layout constraints in the XIB file: (Note although not included in the screen shots the name and by label are keeping with the constraints fine)

enter image description here

Community
  • 1
  • 1
sam_smith
  • 6,023
  • 3
  • 43
  • 60

2 Answers2

1

Have you tried to use

BSettingsCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bSettingsCellIdentifier forIndexPath: indexPath];

instead?

Marcelo Ribeiro
  • 1,718
  • 1
  • 13
  • 27
0

Although the solution was pretty mundane it did help me so I'll post it here.

Basically the solution was to delete what I had done and add it in going through each step. Although at the end it appeared exactly the same it worked. I think that sometimes there are bugs which creep into Xcode and cause problems. Often just reseting the working will fix those.

When attempting to solve this problem I was running on the assumption that the error was somewhere in my code and I probably spent too long trying to find the error. In the end spending a few minutes redoing it a) often allows you to see the error as you can see the step you made wrong b) It can reset any bugs caused by Xcode.

Hope this helps anyone having similar frustrations in future

sam_smith
  • 6,023
  • 3
  • 43
  • 60