0

I am creating an image polling app on iOS. The feed will be instances of the entry similar to the posted image(without borders). I created a custom UITableViewCell using PureLayout. However, the image (which is loaded asynchronously) doesn't show up.

enter image description here

I tried things like invalidateIntrinsicContentSize in the image completion handler but didn't work. I am new to AutoLayout and I am following this sample code

Here is my constraints setup for the image and the 2 top labels

- (void) updateConstraints {
    if (!self.didSetupConstraints) {

        self.contentView.bounds = CGRectMake(0.0f, 0.0f, 99999.0f, 99999.0f);

        [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
            [self.questionLabel autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
        }];
        [self.questionLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kLabelVerticalInsets];
        [self.questionLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
        [self.questionLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];


        [self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.questionLabel withOffset:kLabelVerticalInsets];

        [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
            [self.nameLabel autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
        }];
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];
        [self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kLabelVerticalInsets];



        [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
            [self.mainImageView autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
        }];

        [self.mainImageView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameLabel withOffset:kLabelVerticalInsets];

        [self.mainImageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
        [self.mainImageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
        [self.mainImageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];


        self.didSetupConstraints = YES;
    }

    [super updateConstraints];
} 
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Ahmed Nawar
  • 1,315
  • 2
  • 9
  • 16
  • You seem to pin the bottom of `self.nameLabel` to its superview? That looks wrong to me as you later pin the top of the imageview to the bottom of it. This line: `[self.nameLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kLabelVerticalInsets];` – Rory McKinnel Oct 22 '15 at 22:04
  • Thanks! I removed this line and now the images show up, however, there is a massive gap between (image, name label) and (image, cell bottom). Any ideas? – Ahmed Nawar Oct 22 '15 at 22:07
  • Why are you setting the contentView `bounds` to a huge size? All you should need are the constraints. Try removing that line as well. You also do not set the height for anything, so it may well be struggling to size the cell. The cell auto layout needs to be able to calculate the height of all views to calculate the height of the cell. So I think you need to set the height constraints for the nameLabel, questionLabel and mainImageView. – Rory McKinnel Oct 22 '15 at 22:19
  • So I removed the line but still the same. Regarding the height, shouldn't it calculate the height automatically? – Ahmed Nawar Oct 22 '15 at 22:24
  • I guess that shows it was not needed 8^). I think you need heights for the cell views. Then the cell will set the contentView height to be the sum of the heights. Try adding constraints to set the height of the two labels and image view to test numbers for now and see what happens. Are you not getting constraint error messages in the console? – Rory McKinnel Oct 22 '15 at 22:26
  • But how can I know the image height? They are loaded from the server. I went to PureLayout so that I can have table cells with dynamic heights – Ahmed Nawar Oct 22 '15 at 22:28
  • Well I think you answered it. You need to set a placeholder height for when there is no image for the cell in the data model. When the image loads, update your data model and reload the cell. Don't get sucked into manipulating cells. Always update the date model and reload the affected row. You also have to make sure your cell can cope with being reused. – Rory McKinnel Oct 22 '15 at 22:42

0 Answers0