Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content size issue for UITableView in UIscrollView

I actually create a UIScrollView with a UITableView inside it with interface builder. I set the content size of my UIScrollView with:

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

and I set my file's owner view to the UIScrollView

I created an IBOutlet UITableView and I set it in the interface builder.

When the view is loaded. it displays the correct content size for my UIScrollView. But it doesn't display the correct content size for my UITableView. Besides when I change the content size of my UIScrollView, it changes the content size of my UITableView as if both content size were related.

Anyone knows how can I keep the content size of my table view that I set in the interface builder?

like image 569
kschaeffler Avatar asked Jan 09 '12 13:01

kschaeffler


2 Answers

A UITableView is a UIScrollView. There are very few times when it would be useful to embed a UITableView inside a UIScrollView.

Assuming you really do want this, you'll need to do the following somewhere. Probably in your UIViewController in methods like viewDidAppear: or somewhere you after you populate the table view.

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling).
CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling
tableView.frame = tableFrame;

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view.
// For the purposes of this example, I'm assuming the table view is in there alone.
scrollView.contentSize = tableView.contentSize;
like image 188
DBD Avatar answered Nov 13 '22 14:11

DBD


Example to dynamically manage the TableView Height along with ScrollView, works in Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Custom Cell
        let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell

        //tableList is the OutLet for the table
        tableList.sizeToFit()
        // Set the size of the table to be the size of it's contents
        // (since the outer scroll view will handle the scrolling).
        let height = tableList.frame.size.height
        let pos = tableList.frame.origin.y
        let sizeOfContent = height + pos + 130
        //scrollView is the Outlet for the Scroll view
        scrollView.contentSize.height = sizeOfContent
        scrollView.sizeToFit()

        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }

References to:

  • Setting the ContentSize for ScrollView based in TableView ContentSize.
  • You must use sizeToFit before using contentSize.
like image 21
Abhijeet Avatar answered Nov 13 '22 14:11

Abhijeet