> For the complete documentation index, see [llms.txt](https://jacky-chen.gitbook.io/jackychen/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jacky-chen.gitbook.io/jackychen/user-interface/cell-zhan-kai-shou-he-xiao-guo.md).

# Cell 展開收合效果

* [**Expanding UITableView cells using only constraints in Swift**](https://medium.com/@alexiscreuzot/expanding-uitableview-cells-using-only-constraints-in-swift-f40b13206ea3)
* [**我做的範例**](https://gitlab.com/jackychenkiki/ExpangdingTableViewCell)
  * tableView
    * ViewController 的 viewDidLoad 加上

      ```swift
      tableView.estimatedRowHeight = 60
      tableView.rowHeight = UITableView.automaticDimension
      ```
  * collectionView
    * ViewController 的 viewDidLoad 加上

      ```swift
      let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
      flowLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width - 20, height: 200)
      ```
    * ExpandingCollectionViewCell 加上

      ```swift
      override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes
      {
            setNeedsLayout()
            layoutIfNeeded()
            let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
            var frame = layoutAttributes.frame
            frame.size.height = ceil(size.height)
            layoutAttributes.frame = frame
            return layoutAttributes
      }
      ```
  * storyboard    &#x20;
    * tableViewCell 下的 contenView 加入 stackView，再放進 titileLabel 和 contentStackView
    * collectionView 本身沒有 contentView，所以自己加一個 UIView 充當 contentView，再重複上述步驟
    * contentStackView 再放進 nameLabel 和 ageLabel
    * hugging priority 要設定
    * 最外層的 stackView 要設定 bottom constraint，並且權重設定**小於1000**
    * 藉由操作 contentStackView\.isHidden 達到展開收合效果
