With multiple UITableViews in a single UIScrollvie

2019-07-31 02:15发布

问题:

I have 3 uitableviews in a single uiscrollview. Each uitableview is full length and not scrollable so that the outer uiscrollview scrolls them together. This works fine except the uitableviews believe all cells are visible so that all are created up front. Even this is acceptable except each call has an image view (a thumbnail) that is loaded asynchronously from a url. I am trying to figure out how to limit the image loading to only visible cells but still allow the user to scroll the outer uiscrollview (thus mimicking the uitableview behavior).

The alternative design of a single table with cells that show 3 cells each doesn't work (based on other design requirements) so I am stuck with some way to limit the image downloads. The largest number of cells will be 125 or so. The uiscrollview delegate doesn't seem to have enough calls to allow updating cells on the fly but I could be wrong. Any ideas?

回答1:

Maybe just do a custom check : if the tableView is not visible (because not in the bounds of your scrollView), then do not load the images (or the cells) of the tableView in "cellForRowAtIndexPath".

If the tableView is visible, then call reloadData and display the images.

You can check all this with the scrollViewDelegate methods.



回答2:

TableViewDatasource Protocol implements: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

for a full table that is not in bounds you can limit update simply by comparing the tableview pointer parameter to your actively showing tableviews and skip your non-visible tableviews.

For Cells: This is a more difficult one considering that you are implementing 3 on the same scrollview. This would be a bit easier if you implemented 3 separate tableviews each that are standardly implemented. The reason for this statements is, that the routine if implemented above by Apple's protocol really does only get called for the cells that are currently needing to be on screen. In this way you could implement your image background loader inside of the above defined routine, and you would indeed get what you wish. I have done this and it does work.

Another answer: perhaps you should look into a custom tableview where you define your own custom look and feel for a single tableview that incorporates all the information you wish into this single table thus allowing you to implement the other half stated just above.

To give a better answer, I think I would have to dig deeper into what you are attempting to ultimately accomplish.



回答3:

Appears I can simply use the UIScrollViewDelegate scrollViewDidScroll call and then use the scrollview's contentOffset.y to trigger the thumbnail for any cells which are visible (or about to be) using tableView indexPathsForRowsInRect for each tableview. My cell subclass has a method to trigger the thumbnail download.

The scrollViewDidScroll delegate method seems to be called for every pixel as you scroll which is perfect. I thought it might be too slow but so far it's not a big deal. The only issue to make sure I always check the visible cells if I sort them or something.