22

Can UIStackView be used to resize, reposition, or collapse views within a UITableViewCell?

Rich
  • 8,108
  • 5
  • 46
  • 59
  • I think so, because there is a contentView under the UITableViewCell – Frans Raharja Kurniawan Jun 09 '15 at 01:27
  • 1
    I did use one. I ran into the silliest issue ever. It was because I had the distribution set to `.fillProportionately`. Some rows were just magically not showing or disappearing after I scrolled. For *my* case the solution was to change the distribution to `.fill` – mfaani Nov 27 '17 at 04:26
  • @Honey Saved my day. I was struggling with this for almost 2-3 hours now. Thanks mate!! – letsbondiway Jan 22 '18 at 11:58
  • I built this example, It's a tableView that use a cell that contains a stackView and the views loaded in the stackView are gotten from a nib file. I hope it helps https://github.com/Joule87/stackView-within-TableViewCell – Joule87 Aug 01 '19 at 00:49

2 Answers2

25

Yes its possible.
Take a look at the WWDC15 video. They demonstrate the use of a UIStackView within a UITableViewCell. It's around 5:00

WWDC 15 - Implementing UI Designs in Interface Builder

user023
  • 1,450
  • 2
  • 17
  • 21
4
  • You can use, because UIStackView is a subclass of UIView.
  • We can create a UIStackView with all its arrangedViews and layout properties, place it as a contentview of UITableViewCell.
raja
  • 1,151
  • 7
  • 9
  • 17
    "the height of the cell have to be calculated separately" - this is not correct. The table view will calculate the cell height for you if you a) add the stack view to the cell's content view, b) create constraints that pin the stack view to all four of the content view's edges, and c) set the table view's `estimatedRowHeight` property to something other than 0 (I usually use 2). – Greg Brown Sep 30 '15 at 13:45
  • This is what I'm seeing @memmons, have you had any more luck getting it to work? Or have you got a link to Apple docs that specifically say that it doesn't work? – Robert Atkins Apr 11 '16 at 09:08
  • 2
    It does work for me on iOS 9. In addition to GregBrown's comment set `rowHeight = UITableViewAutomaticDimension`. – Sebastian Apr 25 '16 at 12:21
  • I can confirm it is working properly in iOS 10 (at least) – vilanovi Oct 12 '16 at 18:24
  • Magic! I was missing this and couldn't work out for the life of me why it wouldn't work "c) set the table view's estimatedRowHeight property to something other than 0"! – Inti Nov 14 '17 at 13:12
  • 2
    Can someone post some code to achieve b) create constraints that pin the stack view to all four of the content view's edges? – 7ball Aug 17 '18 at 06:29