14

Update

I have revised the question completely after my latest findings.

Goal

My goal is to implement the following effect:

  1. There is a simple table view
  2. The user selects a row
  3. The selected row expands, revealing another label below the original one

Please note that I am aware, that this can be achieved by inserting/deleting cells below the selected one, I already have a successful implementation using that method.

This time, I want to try to achieve this using Auto Layout constraints.

Current status

I have a sample project available for anyone to check, and also opened an issue. To summarize, here's what I've tried so far:

I have the following views as actors here:

  • The cell's content view
  • A top view, containing the main label ("main view")
  • A bottom view, below the main view, containing the initially hidden label ("detail view")

I have set up Auto Layout constraints within my cell the following way (please note that this is strictly pseudo-language):

  • mainView.top = contentView.top
  • mainView.leading = contentView.leading
  • mainView.trailing = contentView.trailing
  • mainView.bottom = detailView.top
  • detailView.leading = contentView.leading
  • detailView.trailing = contentView.trailing
  • detailView.bottom = contentView.bottom
  • detailView.height = 0

I have a custom UITableViewCell subclass, with multiple outlets, but the most important here is an outlet for the height constraint mentioned previously: the idea here is to set its constant to 0 by default, but when the cell is selected, set it to 44, so it becomes visible:

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    detailViewHeightConstraint.constant = selected ? detailViewDefaultHeight : 0
    UIView.animateWithDuration(0.3) {
        self.layoutIfNeeded()
    }
}

I have the following result:

Current status

So the effect is working, but not exactly how I originally imagined. Instead of pushing the main view up, I want the cell's height to grow when the detail view is shown, and shrink back when it's hidden.

I have examined my layout hierarchy during runtime:

  • The initial state is OK. The height of the content view is equal to the height of my main view (in this case, it's 125 points).

Screenshot initial

  • When the cell is selected, the height constraint of the detail view is increased to 44 points and the two views are properly stacked vertically.But instead of the cell's content view extending, but instead, the main view shrinks.

Screenshot detailed

Question

What I need is the following: the height of table view cell's content view should be equal to

  • the height of the main view, when the detail view's height constraint is 0 (currently this works)
  • main view height + detail view height when the detail view's height constraint is set properly (this does not work).

How do I have to set my constraints to achieve that?

4

2 に答える 2

-1

これは obj-c にありますが、それを処理できると確信しています。

あなたのviewDidLoad:

self.tableView.estimatedRowHeight = self.tableView.rowHeight; self.tableView.rowHeight = UITableViewAutomaticDimension;

これにより、tableView のセルフサイズのセルが有効になり、iOS8 以降で動作するはずです

于 2015-05-07T07:30:13.903 に答える