UITableView how to continue scrolling after reload - ios

I have a UITableView in my application. When the table is scrolled almost to the end, I download more data and reload the table.
If the tableView was scrolling, at the time I call [tableView reloadData]; scrolling stops. How can I achieve effect of not stopping the scroll meanwhile reloadData? I think I need to somehow save scrolling speed and then restore it, but how to do this?
P.D. I really searched this question before asking.

I thing, this method (insertRowsAtIndexPaths:withRowAnimation:) is the key.
There is a use case and a nice tip described on SO: the use case and the tip.

Since UITableView is subclass of UIScrollView, you can use UIScrollViewDelegate's scrollViewWillEndDragging:withVelocity:targetContentOffset:
method to determine how far it is supposed to scroll and then after table reload call setContentOffset:animated: with that target offset (or beginning/ending of tableview if it becomes smaller) to simulate continued scrolling
EDIT: since your targetContentOffset is probably going to be CGRectZero, you will have to recalculate it somehow using velocity from the same method

if i am not wrong you are looking for a function call Lazy load. I can recommend you to search SVPullToRefresh
here!

Related

Update UITableViewCell on UITableView scroll

I'm currently implementing swiping functionality for my custom table view cell and I came across the problem. I want my UIScrollView embedded in cell to reset its contentOffset on table view scroll. The only possible solution I came up with at the moment is to retrieve visibleCells from table view on each scrollViewDidScroll call, cast it to my type and then apply modifications to each of them but I think it might be quite costly and will probably affect performance on older devices. Is there any better way to do that? Thanks in advance!
You could use NotificationCenter.
Post a notification to NotificationCenter within ´scrollViewDidScroll´
within your cell’s subclass, use ´NotificationCenter.default.addObderver() and reset the contentOffset whenever this notification has been received.

How to avoid didSelectRowAtIndexPath being trigged when user tries to scroll bounce-disabled UITableView?

In my application, there's a View controller with bounce-disabled UITableView that covers the entire screen.
However, there is a situation where the table view contains only few items, which the sum of their heights is pretty equals to the screen's height.
As a result the table doesn't scroll (which is fine).
When the user tries to scroll in this situation, it triggers the didSelectRowAtIndexPath of the start scrolling point. How can I avoid this?
One easy solution is to allow bounce effect :D
Couldn't find any other solution :(
to make a better experience make the bounce situational
only allow tableView bounce if the (cellHeight * numberOfCells) < self.tableView.frame.size.height
Looks like some sort of bug in framework (excuse me if it is not). I got this issue today morning and now I am not getting it.
Try adding the below line of code to the cell.
cell.selectionStyle = UITableViewCellSelectionStyleNone
That is the only change I made to the tableview today, and not getting the issue any more. Reverting the change is not reintroducing the issue. Hence I am suspecting a framework issue.

UICollectionview Scrolling while adding Items is super laggy

I have this very nice collectionview that loads pictures from my server. Now, if the pictures are loading slowly, the collectionview is actually scrollable, but if the pictures are loading fast, instead, the collectionview is basically frozen until all the cells are finally loaded.
I'm pretty sure this is something related to the fact that I'm using dispatch_async to update the ui (inserting cells and sections), so I'm wondering if there's a way to update the ui that still allows me to scroll the collectionview while the items are being added.
Is there anything I'm missing?
Thanks in advance for your help!
The easiest way to solve this issue is to use SDWebImage an UIImageView alternative , and call the sd_setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.
You can find it here: https://github.com/rs/SDWebImage
So remove any dispatch_async, set right away the number of cells on this method:
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
Scroll performance is going to be good with this tool

Implementing estimatedHeightForRowAtIndexPath: causes the tableView to scroll down while reloading

I've implemented 'infinite scrolling' on one of my projects and I was playing around with the new estimatedHeightForRowAtIndexPath: delegate method. Once I implemented that delegate method, my tableView jumps (a.k.a scroll to the bottom) whenever i call reloadData (which happens when i add a new set of rows.)
Without that method, my tableView stays in place and it adds the additional rows to the bottom of the tableView without any scrolling.
I'm calling the [tableView reloadData] and not the other methods (insertRowsAtIndexPaths:). I don't call the beginUpdates or endUpdates since I'm reloading the whole table.
Has anyone experienced this ? I
So here's what i did to reduce the jumping while reloading the tableView.
In the estimatedHeightForRowAtIndexPath: I started returning a better estimated height. The more accurate the height is, lesser the jumping while adding new rows to the bottom.
I also started caching the calculated height of the cell from heightForRowAtIndexPath: in a dictionary and return the cached value the next time it calls the estimatedHeightForRowAtIndexPath:.
Hope this helps anyone who encounters this issue.
I filed a BugReport quite a while ago (16472265) and today got the response that it's fixed in iOS8. Tried it out and it works now as expected. No more jumping of the tableview! Wohoooo
Check your condition in this scrollView Delagete method,
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

infinite scroll in tableview using footer

I am trying to support infinite scroll in tableview.
I know tableview lets scroll infinitely, but my case is different.
Initially, I will load 30 items in my tableview as the user scrolls down close to 30th element, I will make a http request to get next 30 items so its not like I am loading 60 items at once.
My way about going to do this is when my table is initally filled with 30 items, once the footerview is seen, then I will request the next 30 items.
My question is how to detect footerview is seen? Is there something like 'will appear' for just footerview?
Let me know if there is other better way of completing my scenario above
Thanks!!
A UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate.
So you can set yourself as the tableViewDelegate and implement scrollViewDidScroll:. You can then either check the scrollView.contentOffset yourself, or call [myTableView visibleCells]. I am sure there are various other methods as well.
The behavior you are trying to implement is known as pull-to-refresh.
There are several implementation of it already available, e.g.:
https://github.com/leah/PullToRefresh
https://github.com/enormego/EGOTableViewPullRefresh
Give it a try, or read the code the learn the details. If you are curious, the way the trick is done is by implementing the delegate methods for the scroll view and checking the value of scrollView.contentOffset.y.

Resources