0

I've got a UIScrollView, on the top there are some information sources and on the button there is a a UITableView. The table view cannot be scrolled, so to be capable of displaying a large number of cells I need to do the manual calculation stuff. That's what I actually do (you can see in the code below). Meanwhile, the container for the table view should also grow with it. I try to change these parameters while loading a view but nothing really changes...

This is how it looks like. In the IB.

enter image description here

The struct.

enter image description here

And the code.

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
    }
}

So, what could be the reason? How to make what I want? Frankly, I don't feel quite familiar with UIScrollView but with a few tutorials the image got constructed.

2 Answers 2

1

Basically, this is a pretty bad way to follow. It could be applied only if you had a very specific features. Generally, you can simply put everything inside of a UITableView. Just create a UIView instance, add subviews, make layout stuff and assign this view as tableView.tableHeaderView. It works for me, hope for you too! :)

Sign up to request clarification or add additional context in comments.

Comments

0

A UITableView will scroll automatically - but it can't because you have set it's height to the height of all of the cells. Set the height to screen height and all will be well.

The UIScrollView is unnecessary, but you have done the same here - you have set the scroll view frame height and content height to the height of the table view. Setting just the content height would enable the scrollview to do its job.

Happy coding.

1 Comment

Thank you for the reply! The point is to make a non-scrollable table view but to provide all the cells with the help of UIScrollView...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.