繁体   English   中英

UITableView内的Swift垂直UICollectionView

[英]Swift vertical UICollectionView inside UITableView

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

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

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

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

我在那里遵循了一些教程和指南,并看到了UITableViewAutomaticDimension许多用例,但无法使其正常工作。

编辑:这是我的情节提要的样子: My StoryBoard这是我的主要ViewController代码:

class FeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var stationArray = Array()

    override func viewDidLoad() {
        super.viewDidLoad()d
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FeedHorizontalTableViewCell
            *Some code*
            return cell
        } else {
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2") as! FeedVerticalTableViewCell
            *Some code*
            cell2.setNeedsLayout()
            return cell2
        }
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 50
        }

        return 0
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 500
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.section == 0 {
                if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") {
                    return cell.frame.height
                }
                return 280
            } else {
                return UITableViewAutomaticDimension
            }
    }
}

这是我的垂直UITableViewCell代码:

class FeedVerticalTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!

    var stations = Array() {
        didSet {
            collectionView.reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return stations.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedVerticalCell", for: indexPath) as! FeedVerticalCollectionViewCell
        *some code*
        return cell
    }
}

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

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

     class DynamicCollectionView: UICollectionView { override func layoutSubviews() { super.layoutSubviews() if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) { self.invalidateIntrinsicContentSize() } } override var intrinsicContentSize: CGSize { return collectionViewLayout.collectionViewContentSize } } 
  2. 在“界面”构建器中,将您的collectionView的类更改为DynamicCollectionView(子类UICollectionView)。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM