1
votes

I currently have a view, uisegmentedcontrol, and another view all inside of a scrollview. The last view swaps between two uitableviews based on the selected index in the uisegmentedcontrol. To make all the elements scroll together, I make the uitableview height the same as it's content size. However, when I do that, all of the cells are loaded because they are "visible" within the scrollview even though they're not visible on the screen. If there is a large row count, this locks up the screen and is slow to load on some phones. Is there any way around this? Is there any way to make the tableview only call tableView:cellForRowAtIndexPath: when it's actually visible on the screen instead of the visible in the scroll view?

I know that using headerView and footerView is an option, but that becomes a little weird because the top view and uisegementedcontrol should be the same for both tableviews. I also have other screens where I have two uitableviews (with both of them having sibling views above and below) inside a uiscrollview and I need everything to scroll together

EDIT: Here is the layout of the screen in the screen with two UITableViews. Everything will be contained in one scroll view:

|-------------------------------|
|UIView (contains title of page)|
|------------------------------ |
|UITableView
|
|
|
|
|
|------------------------------
|UIButton
|
|-----------------------------
|
| UITableView
|
|
|
|
|
|
|
|
|-----------------------------

2

2 Answers

4
votes

Based on my personal experience, there is no way to do what you ask. Apple does not recommend embedding tableviews inside of scrollviews for the reasons mentioned above.

What is visible or considered visible is based on the content size vs visible rect/content offset. UITableView inherits from UIScrollView which has a dynamic contentOffset property. This contentOffSet property changes as you scroll up and down the tableview and is used to determine what cells to load. If the contentOffset is the same as the table size then it thinks the entire table is visible and will load all the cells at once.

0
votes

When you want to switch UITableViews, remove the header and footer views from that UITableView and add them to the other one that you're going to display. That way you won't have the duplicate top view and segmented control issue.

As for your other issue of wanting to have two table views scroll together, it would be best to see a mockup of what you want this to look like, but I think the best way to accomplish this would be using one table view where you have a custom cell that you design to look like it's split in two. Then when you scroll, it'll look like two table views scrolling together.

EDIT:

Based on your addition to your question, I would still say that it should just be one UITableView. The UIView at the top should probably be the header of the UITableView (but doesn't have to be if you want it to always be visible), and the two parts that you have labeled as table views should each be a section of one table view. The UIButton could either be contained in a cell in a section in between, or it could be the header of the second section (or the footer of the top section). I would probably go for having it be a cell in its own section. That way you would have three sections to your table view, one for the top table content, one for the button, and one for the bottom table content.