【问题标题】:Create a dynamically sized scrollView with 3 dynamically sized tableViews within?创建一个动态大小的滚动视图,其中包含 3 个动态大小的 tableViews?
【发布时间】:2017-06-02 07:03:55
【问题描述】:

我正在创建一个可滚动的视图,它是购物车的结帐部分。

这是视图布局

这是一个可滚动的视图,里面有一个 UIView,其内容大小应根据其中每个表格视图的大小自行调整,UIView 内的每个表格视图应根据内容大小调整其高度,内容大小取决于数量篮子和产品。 (请注意,我想要调整整个 tableView 的大小,而不仅仅是其中一个单元格)。

我检查了这个答案Change UITableViewHeight Dynamically,它提供了一些关于如何使我的 UITableViews 自动调整大小的建议,但我需要 UIView 和内部的 TableViews 都可以动态调整大小。

非常感谢任何帮助。

【问题讨论】:

  • 什么不能将三个表视图中的所有数据合并到一个表视图中?
  • 因为他们每个人持有不同的数据,他们有不同的状态。如果包含 Canastas 的数组为空,则应发送带有空购物车图标的笔尖,如果 Canastas 不为空,则应发送带有 canastas 单元格的笔尖,与 Productos 相同,最后一个 tableview 是之前 tableViews 中包含的产品的简明摘要。每个视图都有自己的功能,每个视图的大小应该是数组中包含的项目总数的大小,滚动视图的大小应该相应地调整。这就是为什么。
  • @OctavioRojas;每个 tableView 是可滚动的,还是它们会在超级滚动视图内显示 FULL 大小?
  • 它们是不可滚动的(这就是 scrollView 的用途),如果有 1 个单元格或者如果有 10 个单元格,它们应该能够动态调整大小,它们应该相应地调整它们的大小,并且 scrollView 应该也进行调整,以便能够滚动浏览它们。
  • 您可以使用单个表格视图并将其拆分为某些部分。 no product 标签也是一个单元格...

标签: ios swift uitableview uiview uiscrollview


【解决方案1】:

我不明白你为什么不想将它们合并到一个表中..

无论如何.. UIScrollView 不适用于自动布局。您必须添加与滚动视图的父级相同尺寸的 contentView 或具有固定大小。

现在不碍事了,您将 tableViews 约束到 scrollView 的 contentView,然后覆盖控制器的 viewDidLayoutSubviews。在该函数中,您需要获取每个 tableView 的 contentSize 并将每个 tableView 的高度限制为其内容大小。这将使表格全尺寸且不可滚动。

由于 scrollView 会根据其内容自动调整大小,因此您无需执行任何其他操作。

或者,如果您不想使用AutoLayoutScrollView,可以将UIScrollView contentSize 设置为控制器viewDidLayoutSubviews 中3 个表的contentSize 之和。

注意:我没有 tableViews 的任何示例动态内容,但如果您希望它们使用动态行大小,您需要这样做:

table.estimatedRowHeight = 300  //Estimation of the average size of the rows.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    return UITableViewAutomaticDimension
}

现在对于具有硬编码行高的实际代码(因为我没有动态测试数据):

//
//  ViewController.swift
//  SO
//
//  Created by Brandon T on 2017-01-17.
//  Copyright © 2017 XIO. All rights reserved.
//

import UIKit


class AutoLayoutScrollView : UIScrollView {
    private(set) weak var contentView: UIView!
    private var hConstraint: NSLayoutConstraint!
    private var vConstraint: NSLayoutConstraint!

    init() {
        super.init(frame: .zero)
        self.layout()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layout()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layout()
    }

    private func layout() {
        let view = UIView()
        self.contentView = view
        self.addSubview(self.contentView)

        self.contentView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        self.contentView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        self.contentView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        self.contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        self.contentView.translatesAutoresizingMaskIntoConstraints = false
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()

        if let parent = self.superview {
            self.leftAnchor.constraint(equalTo: parent.leftAnchor).isActive = true
            self.rightAnchor.constraint(equalTo: parent.rightAnchor).isActive = true
            self.topAnchor.constraint(equalTo: parent.topAnchor).isActive = true
            self.bottomAnchor.constraint(equalTo: parent.bottomAnchor).isActive = true
            self.translatesAutoresizingMaskIntoConstraints = false

            self.hConstraint = self.contentView.widthAnchor.constraint(equalTo: parent.widthAnchor)
            self.vConstraint = self.contentView.heightAnchor.constraint(equalTo: parent.heightAnchor)
            self.hConstraint.isActive = true
            self.vConstraint.isActive = true
        }
    }

    func setHorizontalScrollEnabled(enabled: Bool) {
        self.hConstraint.isActive = !enabled
    }

    func setVerticalScrollEnabled(enabled: Bool) {
        self.vConstraint.isActive = !enabled
    }
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private var scrollView: AutoLayoutScrollView!
    private var topTableView: UITableView!
    private var middleTableView: UITableView!
    private var bottomTableView: UITableView!

    private var topTableHeight: NSLayoutConstraint!
    private var middleTableHeight: NSLayoutConstraint!
    private var bottomTableHeight: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.layout()
    }

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

    func layout() {
        //Init Views
        self.scrollView = AutoLayoutScrollView()
        self.topTableView = UITableView(frame: .zero, style: .plain)
        self.middleTableView = UITableView(frame: .zero, style: .plain)
        self.bottomTableView = UITableView(frame: .zero, style: .plain)

        self.registerClasses()

        //Add Views
        self.view.addSubview(self.scrollView)
        self.scrollView.contentView.addSubview(self.topTableView)
        self.scrollView.contentView.addSubview(self.middleTableView)
        self.scrollView.contentView.addSubview(self.bottomTableView)

        //Layout Views
        self.topTableView.leftAnchor.constraint(equalTo: self.scrollView.contentView.leftAnchor).isActive = true
        self.topTableView.rightAnchor.constraint(equalTo: self.scrollView.contentView.rightAnchor).isActive = true
        self.topTableView.topAnchor.constraint(equalTo: self.scrollView.contentView.topAnchor).isActive = true
        self.topTableView.translatesAutoresizingMaskIntoConstraints = false

        self.middleTableView.leftAnchor.constraint(equalTo: self.scrollView.contentView.leftAnchor).isActive = true
        self.middleTableView.rightAnchor.constraint(equalTo: self.scrollView.contentView.rightAnchor).isActive = true
        self.middleTableView.topAnchor.constraint(equalTo: self.topTableView.bottomAnchor).isActive = true
        self.middleTableView.translatesAutoresizingMaskIntoConstraints = false

        self.bottomTableView.leftAnchor.constraint(equalTo: self.scrollView.contentView.leftAnchor).isActive = true
        self.bottomTableView.rightAnchor.constraint(equalTo: self.scrollView.contentView.rightAnchor).isActive = true
        self.bottomTableView.topAnchor.constraint(equalTo: self.middleTableView.bottomAnchor).isActive = true
        self.bottomTableView.bottomAnchor.constraint(equalTo: self.scrollView.contentView.bottomAnchor).isActive = true
        self.bottomTableView.translatesAutoresizingMaskIntoConstraints = false

        self.topTableHeight = self.topTableView.heightAnchor.constraint(equalToConstant: 0)
        self.middleTableHeight = self.middleTableView.heightAnchor.constraint(equalToConstant: 0)
        self.bottomTableHeight = self.bottomTableView.heightAnchor.constraint(equalToConstant: 0)


        //Set Views Properties
        self.scrollView.setVerticalScrollEnabled(enabled: true)

        self.topTableView.delegate = self
        self.topTableView.dataSource = self
        self.middleTableView.delegate = self
        self.middleTableView.dataSource = self
        self.bottomTableView.delegate = self
        self.bottomTableView.dataSource = self


        //Display Views
        self.topTableView.reloadData()
        self.middleTableView.reloadData()
        self.bottomTableView.reloadData()
    }

    func registerClasses() {
        self.topTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
        self.middleTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
        self.bottomTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        //Update tables constraints to full size.
        self.topTableHeight.constant = self.topTableView.contentSize.height
        self.middleTableHeight.constant = self.middleTableView.contentSize.height
        self.bottomTableHeight.constant = self.bottomTableView.contentSize.height

        self.topTableHeight.isActive = true
        self.middleTableHeight.isActive = true
        self.bottomTableHeight.isActive = true
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (tableView == self.topTableView) {
            return 10
        }

        if (tableView == self.middleTableView) {
            return 15
        }

        if (tableView == self.bottomTableView) {
            return 3
        }

        return 0
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (tableView == self.topTableView) {
            return 100
        }

        if (tableView == self.middleTableView) {
            return 250
        }

        if (tableView == self.bottomTableView) {
            return 500
        }

        return 0.0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)

        if (tableView == self.topTableView) {
            cell.contentView.backgroundColor = UIColor.red
        }

        if (tableView == self.middleTableView) {
            cell.contentView.backgroundColor = UIColor.green
        }

        if (tableView == self.bottomTableView) {
            cell.contentView.backgroundColor = UIColor.blue
        }

        return cell
    }
}

【讨论】:

  • 太棒了,这就是我正在寻找的答案,非常感谢!我不会将它们全部组合在一个表中,因为我在不同的地方有视图,(它们不是连续的),如果你知道一种方法可以让同一个表视图的不同部分分散在视图的不同部分让我知道怎么做,我会用一个 scrollView 来代替。
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
相关资源
最近更新 更多