繁体   English   中英

动态表视图+滚动视图

[英]Dynamic table view + scroll view

我有一个UIScrollView ,在顶部有一些信息源,在按钮上有一个UITableView 表格视图无法滚动,因此要能够显示大量单元格,我需要进行手动计算。 那就是我实际上所做的(您可以在下面的代码中看到)。 同时,用于表格视图的容器也应该随之增加。 我尝试在加载视图时更改这些参数,但实际上什么都没有改变...

这就是它的样子。 在IB。

在此处输入图片说明

结构。

在此处输入图片说明

和代码。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var tableView: UITableView!

    let numberOfCells = 55
    let rowHeight: CGFloat = 40
    let footerHeight: CGFloat = 30
    let headerHeight: CGFloat = 30

    let identifier = "Cell"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.backgroundColor = self.view.backgroundColor
        self.tableView.scrollEnabled = false

        self.automaticallyAdjustsScrollViewInsets = false
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)

        let estimatedHeight = CGFloat(self.numberOfCells) * self.rowHeight + self.footerHeight + self.headerHeight
        self.tableView.frame.size = CGSizeMake(self.tableView.bounds.width, estimatedHeight)
        self.scrollView.contentSize = CGSizeMake(self.view.bounds.width, CGRectGetMaxY(self.tableView.frame))
        self.scrollView.frame.size = CGSizeMake(self.view.bounds.width, CGRectGetMaxY(self.tableView.frame))
    }

    // MARK:- Table view data source

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.numberOfCells
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(self.identifier) as UITableViewCell

        cell.textLabel?.text = "City"
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

        return cell
    }

    // MARK:- Table view delegate

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.rowHeight
    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return self.headerHeight
    }

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return self.footerHeight
    }
}

那么,可能是什么原因呢? 如何做我想要的? 坦白说,我对UIScrollView不太熟悉,但是通过一些教程就可以构造图像。

基本上,这是一个非常糟糕的方法。 仅当您具有非常特定的功能时,才可以应用它。 通常,您可以简单地将所有内容放入UITableView 只需创建一个UIView实例,添加子视图,进行布局,然后将此视图分配为tableView.tableHeaderView 它对我有用,对你也希望! :)

UITableView将自动滚动-但是不能,因为您已经将其高度设置为所有单元格的高度。 将高度设置为屏幕高度,一切都会好起来。

UIScrollView是不必要的,但是您在此处做了相同的操作-将滚动视图框架的高度和内容高度设置为表格视图的高度。 仅设置内容高度将使滚动视图能够执行其工作。

快乐的编码。

暂无
暂无

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

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