Content size issue for UITableView in UIscrollView

I actually create UIScrollViewfrom UITableViewwithin it using the interface builder. I set the size of my content UIScrollViewwith:

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

and I set the file owner view to UIScrollView

I created IBOutlet UITableViewand I installed it in the interface builder.

When the image is uploaded. It displays the correct content size for mine UIScrollView. But it does not display the correct content size for mine UITableView. Also, when I resize my content UIScrollView, it UITableViewresizes my content , as if both sizes of content were connected.

Does anyone know how I can keep the size of the contents of my table view that I set in the interface builder?

+5
source share
4 answers

A UITableViewis UIScrollView. There is very little time when it would be useful to insert UITableViewinside a UIScrollView.

Assuming you really want it, you need to do the following. Perhaps in yours UIViewControllerin methods, for example viewDidAppear:, or somewhere after filling out a table.

// Set the size of your table to be the size of it 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;
+12
source

Example of dynamic height control TableView 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 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
    }

Links to:

+1
source

, .

DBD , contentSize, , , :

self.tableView.contentSize = CGSizeMake(320, scrollHeight);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height);

, , , , . , , , :

int cellCount = [YOURARRAY count];
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14)
{
    //here were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7)
{
     //same as above but it for the iPhone in portrait
     self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight);
}

. autoresizing .xib , - , , .

0

. , , .

CGRect tableFrame = tableView.frame;
tableFrame.size.height = tableView.contentSize.height;
tableView.frame = tableFrame;

scrollView.contentSize = tableView.contentSize;
0

All Articles