gpt4 book ai didi

ios - UITableView 内的 Swift 垂直 UICollectionView

转载 作者:可可西里 更新时间:2023-11-01 00:55:07 25 4
gpt4 key购买 nike

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

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

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

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

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

编辑:这是我的 Storyboard的样子: 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    }}

最佳答案

在 View Controller 中将 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)

关于ios - UITableView 内的 Swift 垂直 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51614952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com