菜鸟教程小白 发表于 2022-12-11 20:35:33

ios - 嵌套表和 Collection View 的自定尺寸单元格


                                            <p><p>我正在尝试构建一个表格 View ,此 TableView 中的一个单元格是一个选项卡小部件。
所以我将这个选项卡小部件创建为 CollectionView。 collectionView 的单元格有不同的 subview ,每个单元格都有自己的大小。</p>

<p>我想要实现的是在 collectionView 中调整单元格大小,但它不起作用。</p>

<p>我会为你提供我使用的方式,以便我可以得到你的帮助。</p>

<pre><code>//MARK: TableView
   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
            return UITableViewAutomaticDimension
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;DetailsTabs&#34;, for: indexPath) as! DetailsTabs /*this cell has 2 collectionViews. The first collectionView is the tabs and the second collectionView is the content that should be diplayed. I gave the first collectionView a fixed height, trailing leading and top. the second collectionView has trailing, leading bottom, and top to the first collectionView*/
      cell.postDetail = postDetail
      cell.widgets = self.Template
      cell.parentController = self
      cell.Shownavigationbar = self.Shownavigationbar

      return cell
}

//MARK: DetailsTabs Class

override func awakeFromNib() {
//code to setUp the collectionViews

//pagesLayout is the UICollectionViewFlowLayout for the second collectionView
       pagesLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width, height: 1)
      }

//MARK: IN collectionView Cells I added this function:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -&gt; UICollectionViewLayoutAttributes {
      setNeedsLayout()
      layoutIfNeeded()

      let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
      var frame = layoutAttributes.frame
      frame.size.height = ceil(size.height)
      layoutAttributes.frame = frame

      return layoutAttributes
    }
</code></pre>

<p>注意:collectionView 单元格的内容可能是 webView 或 tableview。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      return 50
    }

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      return UITableViewAutomaticDimension
    }
</code></pre>

<p>你应该在<em>estimatedHeightForRowAtIndexPath</em>中使用<strong>UITableViewAutomaticDimension</strong></p>

<p>希望这对你来说很好......!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 嵌套表和 Collection View 的自定尺寸单元格,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52329027/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52329027/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 嵌套表和 Collection View 的自定尺寸单元格