2

I have a custom UITableViewCell with UITextView, UICollectionView and UITableView inside. They have a 10 px space between. In some cases I have only UITextView, sometimes only collection view etc.

When no text - I have to move UICollection view up by 10 px. And If I have no text, tableView and collectionView it should be zero height.

- (void)configureMiddleWrapperState
{
    self.middleWrapperHasNote = self.noteTextView.text.length > 0;
    self.noteTextViewTopConstraint.constant = self.middleWrapperHasNote ? 7.f : 0.f;
    self.staffWrapperTopConstraint.constant = self.middleWrapperHasStaff ? 7.f : 0.f;
    self.tagsWrapperTopConstraint.constant = self.middleWrapperHasTags ? 7.f : 0.f;
    BOOL middleWrapperHasAtleastOne = self.middleWrapperHasNote || self.middleWrapperHasStaff ||
    self.middleWrapperHasTags;
    self.middleWrapperLastTenPixelSpaceConstraint.constant = middleWrapperHasAtleastOne ? 7.f : 0.f;
    [self setNeedsUpdateConstraints];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setExcludedPaths];
    [self configureMiddleWrapperState];
}

The result isn't like expected: enter image description here instead of one line. When only text is looks like expected. When Only Text

Constraints become work only if I scroll up and down many times.

Artem Z.
  • 1,243
  • 2
  • 14
  • 36

2 Answers2

1

try add

[self setNeedsLayout];

after

[self setNeedsUpdateConstraints];
Moonkid
  • 881
  • 7
  • 17
  • It will loop - setNeedsLayout forces [self layoutSubviews] – Artem Z. Jan 31 '17 at 13:37
  • So I think you have to move your logic somewere else. Maybe - (void) awakeFromNib will suit. – Moonkid Jan 31 '17 at 13:52
  • In awakeFromNib I don't know how to serve note text etc. Coz It didn't set in awakeFromNib – Artem Z. Jan 31 '17 at 13:54
  • Then create a new method that will configure cell with text and then update constraints - (void) configureWithText:(NSString *) newText; – Moonkid Jan 31 '17 at 14:03
  • I've found the issue. Coz my self.middleWrapperHasTags doesn't set correctly when reuse. By the way thanks for your help – Artem Z. Jan 31 '17 at 14:16
0

I've found the issue. Coz my self.middleWrapperHasTags doesn't set correctly when reuse.

Artem Z.
  • 1,243
  • 2
  • 14
  • 36