0

I'm currently working on a social media app (basic like Facebook) and I'm having the hardest time working with autolayout. I used to do it using frames and stuff, but since my whole project uses autolayout, I'd like to try and make it work that way.

Basically, I've got a cell with some informations on top, a UILabel below (that contains the user's status), a view below (that can contain anything from a UIImage to a web view. It acts as a container) and a bar on the bottom with actions buttons (like/share and stuff).

I use the same cell for any content (text only, image, video and webpage). What I though I would do is when the user only shares a status, I remove the container view (or hide it), and on other cases, I start with :

  1. calculating the status' label height from the status' text using boundingRectWithSize
  2. depending on the feed type (text, image, etc.), I calculate the whole cell height

The cell height work pretty fine. I'm having difficulties with the status text label.

At first, I tried to set the label's height in the cellForRowAtIndexPath using a setFrame. It did not work.

After that, I tried to calculate the label's height directly in the cell. I called this method in the layoutSubviews of the cell

- (void)resizeStatusTextField
{
    NSString* text = self.feedTextLabel.text;
    CGRect rect = [text boundingRectWithSize:CGSizeMake(300, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.feedTextLabel.font} context:nil];
    NSDictionary* views = NSDictionaryOfVariableBindings(_containerView, _feedTextLabel);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[_containerView]-46-[_feedTextLabel(%f)]", rect.size.height] options:0 metrics:nil views:views]];
    [self setNeedsUpdateConstraints];
}

So what I tried to do is settings the feedTextLabel constraints to 46 points below it's container's top and settings the calculated size.

It's seems to work more or less (thought I'm not sure it is the right way to do it) but now whenever I scroll, the text content disappears.

If someone can point out to me the correct way to manage such dynamic constraints inside tableView, it would be much appreciated.

Thanks.

EDIT : I of course read posts like "Using Auto Layout in UITableView for dynamic cell layouts & variable row heights", but I believe my case is pretty peculiar since I got multiple views that can have dynamic heights inside the cell. So please don't mark this post as duplicate ;) Thanks

Community
  • 1
  • 1
Khal
  • 790
  • 5
  • 23
  • First, figure out a layout for your code, just a rough draft. Then, figure out the elements the sizes of which you can calculate. Do that homework and I'd be happy to walk you through to the answer. – duci9y Jul 26 '14 at 09:44
  • Lol thanks, but I already done that, but I guess I'm stuck on the correct layout properties. I saw that if cells had layouts that differs too much (like when I hide a whole view), it was best to create separate reuseIdentifiers too. I'll try that and come back here when I'm done ;) Thx – Khal Jul 26 '14 at 13:24
  • Did not get what you meant by that. Care to elaborate? – duci9y Jul 26 '14 at 13:25
  • Cells have a feed_text (the status the user posts) and can have a feed_contantView (basically a container view in which I add a UIImageView when the user shares a picture, or a UIWebView when he shares a link). When there's only text to share, I thought of hiding the container view, but it seems that it's more efficient if I create 2 cells layout (one for each case) and give them a separate reuseIdentifier. Am I right? – Khal Jul 26 '14 at 13:35
  • Yep, that's the way to go. Custom cells for custom content. – duci9y Jul 26 '14 at 13:39
  • I'll try that tomorrow then ;) Any particular advice or tips on AutoLayout with cells before I digg in? Thanks. – Khal Jul 26 '14 at 13:43
  • When telling the tableview the height for the cell at an index path, dequeue a cell, fill it up with the content stored at that index path, make sure your constraints are vertically tight, `layoutIfNeeded`, and send it a `systemLayoutSizeFittingSize:` to get the minimum height needed to display all content. Also read up on `estimatedHeightForRowAtIndexPath:`. – duci9y Jul 26 '14 at 13:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58068/discussion-between-khal-and-duci9y). – Khal Jul 26 '14 at 17:59

0 Answers0