【问题标题】:Swift vertical UICollectionView inside UITableViewUITableView 内的 Swift 垂直 UICollectionView
【发布时间】:2019-01-07 22:37:43
【问题描述】:

我正在开发一个应用程序,它要求我在 ViewController 中有两种类型的提要 - 水平和垂直。

结构是这样的:ViewController -> UITableView -> 第一段有水平的UICollectionView -> 第二段有垂直的UICollectionView

我的问题是我无法让UITableView 的第二部分(在垂直UICollectionView 内部)具有适当的高度。当我向下滚动时,它会在中间被打断。

重要的是要说 UICollectionView 单元格高度不是固定的。

我遵循了一些教程和指南,看到了许多UITableViewAutomaticDimension 的用例,但无法让它发挥作用。

编辑: 这是我的故事板的样子: My StoryBoard 这是我的主要 ViewController 代码:

类 FeedViewController:UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet 弱 var tableView:UITableView! var stationArray = 数组() 覆盖 func viewDidLoad() { super.viewDidLoad()d } func numberOfSections(in tableView: UITableView) -> Int { 返回 2 } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 返回类别[部分] } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 如果 indexPath.section == 0 { 让 cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FeedHorizo​​ntalTableViewCell *一些代码* 返回单元格 } 别的 { 让 cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2") 为! FeedVerticalTableViewCell *一些代码* cell2.setNeedsLayout() 返回单元格2 } } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 如果部分 == 1 { 返回 50 } 返回 0 } func tableView(_ tableView:UITableView,estimateHeightForRowAt indexPath:IndexPath)-> CGFloat { 返回 500 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 如果 indexPath.section == 0 { if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") { 返回 cell.frame.height } 返回 280 } 别的 { 返回 UITableViewAutomaticDimension } } }

这是我的垂直 UITableViewCell 代码:

类 FeedVerticalTableViewCell:UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { @IBOutlet 弱 var collectionView:UICollectionView! var站=数组(){ 设置{ collectionView.reloadData() } } 覆盖 func awakeFromNib() { super.awakeFromNib() } 覆盖 func setSelected(_ selected: Bool, 动画: Bool) { super.setSelected(选定,动画:动画) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 返回站数.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedVerticalCell", for: indexPath) as! FeedVerticalCollectionViewCell *一些代码* 返回单元格 } }

【问题讨论】:

  • 请提供您的代码以显示您到目前为止所做的工作
  • 我认为您必须计算 CollectionView 高度购买代码并将其传递给 tableView 委托: func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { } (和旋转时使用自定义算法..)无论如何没有示例代码有点奇怪。请忘记“自动”:对于自定义接口,所有这些都必须由编码人员在代码中使用:(
  • 我添加了带有屏幕截图的代码,希望这足以理解我的问题。谢谢!!
  • 从你的问题我理解的意思是:用户界面需要垂直滚动(水平滚动也随之滚动)。如果你想这样把你的UITableView 设置为group type 并在section 标题中设置水平滚动。只使用一个部分
  • 感谢@MathiArasan 的回复,但问题在于垂直tableview 单元格(第二个单元格)的collectionview 高度不正确。

标签: ios iphone swift uitableview uicollectionview


【解决方案1】:

在视图控制器中将 UITableView 单元格高度设置为动态后。

     tableView.estimatedRowHeight = 100
     tableView.rowHeight = UITableViewAutomaticDimension
  1. 为您的垂直collectionView 创建一个子类并覆盖intrinsicContentSize。

    class DynamicCollectionView: UICollectionView {
       override func layoutSubviews() {
       super.layoutSubviews()
       if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
          self.invalidateIntrinsicContentSize()
       }
     }
    
       override var intrinsicContentSize: CGSize {
         return collectionViewLayout.collectionViewContentSize
       }
    }
    
  2. 在 Interface builder 中,将 collectionView 的类更改为 DynamicCollectionView(子类 UICollectionView)。

  3. 设置 UICollectionViewFlowLayout 的估计单元格大小。

        flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2013-05-26
    • 2018-11-09
    • 2018-05-09
    相关资源
    最近更新 更多