Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITableView自调整大小UICollectionView,动态标题作为单元格_Ios_Swift_Uitableview_Uicollectionview_Autolayout - Fatal编程技术网

Ios 使用UITableView自调整大小UICollectionView,动态标题作为单元格

Ios 使用UITableView自调整大小UICollectionView,动态标题作为单元格,ios,swift,uitableview,uicollectionview,autolayout,Ios,Swift,Uitableview,Uicollectionview,Autolayout,我在使用包含UITableView的单元格创建自调整大小的UICollectionView时遇到问题,UITableView的标题具有动态高度的标签 有人能指出我需要在所附的示例项目中更改什么吗 您可以在屏幕截图上看到,表格不适合视图,因为单元格的高度当前是手动设置的 要实现自动调整collectionView单元格的大小,实际上只需要进行一些更改 两个关键点: 单元格必须满足其自身的约束。因此,不要在sizeForItemAt中计算大小,而是确保单元格具有宽度和高度约束。这些可以是动态的,

我在使用包含UITableView的单元格创建自调整大小的UICollectionView时遇到问题,UITableView的标题具有动态高度的标签

有人能指出我需要在所附的示例项目中更改什么吗

您可以在屏幕截图上看到,表格不适合视图,因为单元格的高度当前是手动设置的


要实现自动调整collectionView单元格的大小,实际上只需要进行一些更改

两个关键点:

  • 单元格必须满足其自身的约束。因此,不要在
    sizeForItemAt
    中计算大小,而是确保单元格具有宽度和高度约束。这些可以是动态的,基于内容

  • 将元素添加到集合视图单元格的
    contentView
    ,而不是单元格本身

  • 对于嵌入式非滚动表视图,使用一个子类,该子类根据表的contentSize设置内部内容大小高度。例如:



这里有一个非常好的教程(不是我的):有更详细的解释

我在您提供的项目中实现了这些概念,并将其放在GitHub repo上(以便于查看更改):

结果:


您是否尝试将常量添加到右锚约束?@vishal-anchor本身绝对不是enough@Thunder-不太清楚你说的是错的。。。图像是否太窄?是从屏幕底部延伸出来的行吗?是“Section 2”标签和“Lorem ipsum…”段落顶部之间的垂直间距吗?@DonMag有一个指向工作示例的dropbox链接。基本上如屏幕截图所示,第2节有一个带有标题的UITableView(它基本上只有一个UILabel)。由于主集合视图有一个手动输入的section 2单元格的高度,所以表的大部分被截断。我希望它在集合视图中根据分区大小适当地调整自身大小。我在图像中没有看到任何错误。您的意思是
键3
值3
不可见?请尝试在第1节中的UICollectionViewFlowLayout.AutomaticSizeForiteMat自动调整大小。GitHub链接提供404。尽管如此,谢谢,我们将尝试并批准。@Thunder-再次尝试GitHub链接。。。现在应该可以工作了。
import UIKit

class ViewController: UIViewController {
    static let section1 = "section1"
        static let section2 = "section2"
        private weak var collectionView: UICollectionView!

        override func viewDidLoad() {
            self.navigationItem.title = "Collection View"
            super.viewDidLoad()
            self.setupCollectionView()
        }

        private func setupCollectionView() {
            let flowLayout = UICollectionViewFlowLayout()
            flowLayout.scrollDirection = .vertical
            flowLayout.minimumInteritemSpacing = 0.0
            flowLayout.minimumLineSpacing = 0.0
            let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: flowLayout)
            collectionView.alwaysBounceVertical = true
            collectionView.register(
                SectionHeaderView.self,
                forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                withReuseIdentifier: SectionHeaderView.sectionHeaderId
            )
            collectionView.register(Section1.self, forCellWithReuseIdentifier: ViewController.section1)
            collectionView.register(Section2.self, forCellWithReuseIdentifier: ViewController.section2)
            self.view.addSubview(collectionView)
            self.collectionView = collectionView
            self.collectionView.translatesAutoresizingMaskIntoConstraints = false
            self.collectionView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
            self.collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
            self.collectionView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
            self.collectionView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
            self.collectionView.dataSource = self
            self.collectionView.delegate = self
        }
    }

    // MARK: - UICollectionViewDelegateFlowLayout
    extension ViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            switch indexPath.section {
            case 0:
                return CGSize(width: self.view.frame.width, height: 210.0)
            case 1:
                return CGSize(width: self.view.frame.width, height: 5 * 51.0 + 130.0) // How to enable self-sizing cells for table view inside
            default:
                fatalError("Unsupported section index.")
            }
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
            return CGSize(width: self.view.frame.width, height: 61)
        }
    }

    // MARK: - UICollectionViewDataSource
    extension ViewController: UICollectionViewDataSource {
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 2
        }

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

        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: SectionHeaderView.sectionHeaderId, for: indexPath) as! SectionHeaderView
            switch indexPath.section {
            case 0:
                header.uiLabel.text = "Section 1"
            case 1:
                header.uiLabel.text = "Section 2"
            default:
                fatalError("Unsupported section index.")
            }
            return header
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            switch indexPath.section {
            case 0:
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ViewController.section1, for: indexPath) as! Section1
                return cell
            case 1:
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ViewController.section2, for: indexPath) as! Section2
                return cell
            default:
                fatalError("OverviewController: Unsupported section index.")
            }
        }
}
final class ContentSizedTableView: UITableView {
    override var contentSize:CGSize {
        didSet {
            invalidateIntrinsicContentSize()
        }
    }
    override var intrinsicContentSize: CGSize {
        layoutIfNeeded()
        return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
    }
}