Uitableview scroll to top after reload swift

Infinite scrolling allows users to load content continuously, eliminating the need for pagination. The app loads some initial data and then adds the rest of the information when the user reaches the bottom of the visible content.

Social media companies like Twitter and Facebook have made this technique popular over the years. If you look at their mobile applications, you can see infinite scrolling in action. This is an adaptive technology that performs optimizations targeted to improve scrolling performances.

Data source prefetching provides a mechanism to prepare data before you need to display it. For large data sources where fetching the information takes time, implementing this technology can have a dramatic impact on user experience. Start by downloading the starter project using the Download Materials link at the top or bottom of this tutorial.

Once downloaded, open ModeratorsExplorer. To keep you focused, the starter project has everything unrelated to infinite scrolling already set up for you. In Viewsopen Main. At the moment, tapping on Find Moderators! As the name implies, it returns the list of moderators for a specific site.

uitableview scroll to top after reload swift

Pagination is a common technique for many public APIs. Instead of sending you all the data they have, they send a limited amount; when you need more, you make another request. This saves server resources and provides a faster response.

The response includes the total number of moderators in their system 84 and the requested page 1. With this information, and the list of moderators received, you can determine the number of items and pages you need to request to show the complete list. In Networkingopen StackExchangeClient. Replace the method with this:. Note : In both the success and failure cases, you need to tell the delegate to perform its work on the main thread: DispatchQueue.

uitableview scroll to top after reload swift

Build and run the app. Type stackoverflow in the text field and tap on Find Moderators. Hang on!Tag: iosobjective-cuitableviewcore-datansfetchedresultscontrolle. Ok so what I need is to prevent tableview scrolling when new elements added above current visible row. However, when I'm trying to manipulate contentOffset to keep scroll position when new entries are inserted it starts to freeze UI for like ms which is bad for me.

Contacts: Animations Reload Rows in UITableView (Ep 2)

Can anyone suggest any better faster solution of how to keep tableview at same cell when new items added at the top? And this is the code that I'm using currently to track insertions and shift contentOffset.

It doesn't support animations and different cell size. Ok, after some responses which pointed me in right direction I came up with this solution. It works smooth though does some ugly coordinates math and doesn't support different cell sizes. Hope it will be helpful for someone. In initializeSettingsView add nibView. And then in viewDidload add the hight constraint to settingsView constraints.

uitableview scroll to top after reload swift

Height, relatedBy:. Equal, toItem: nil, attribute:. NotAnAttribute, multiplier: 1. Interesting question, I've never applied auto-layout to a text field so I was curious myself.

UITableView scroll to top with Swift

Upon that, I would check to see if the text field was the first responder. If it was, I set it to be first responder But what I think is happening is that you need to call viewWithTag: on cell. I'd recommend creating proper IBOutlets to hold your imageview and label You can do it using geometry shaders to create billboarding geometry from each vertex on the GPU.

You can then either create the circles as geometry, or create quads and use a circle texture to draw them I recommend the later. But geometry shaders are not extensively supported yet, even You can find a lot of example for both ways, here's a few: use a GIF for cool background video cover iOS In reply to your Roll back those changes, add a InfoPlist.I'm serious that all of these are necessary, even though it seems the first line is not relevant at all.

Oh yeah, also very importantdo NOT use this:. Apple treats this method differently and in my case this only worked when using the full method with animated:FALSE. I also wanted to try this with a nice scrolling animation to the top.

There the content offset methods seemed to still cause some strange animation behaviours. The only way I got this working with a nice animation after some trial and error were these methods in this exact order:. Warning, make sure you have at least 1 row before running the last line of code to avoid crashing :. I am trying to get a UITableview to go to the top of the page when I reload the table data when I call the following from.

Along those same lines, I would also like to be able to go to a specific section when I reload the table data. I tried placing the following, which seems applicable only to the row, before and after the reload, but nothing happens:.

After reading all of the answers here and elsewhere, I finally found a solution that works reliably and does not show the scrolling after the data is loaded. My answer is based on this post and my observation that you need a negative offset if you are using content insets:. If you scroll before reloading and the number of rows decreases, you can have some strange animating behavior. To ensure the scrolling happens after reload, use a method like this:. Oh yeah, also very importantdo NOT use this: self.

The only way I got this working with a nice animation after some trial and error were these methods in this exact order: [self. In swift 3. Vertically align text to top within a UILabel Keep uitableview static when inserting rows at the top.Posted by: admin February 24, Leave a comment. I know no delegate method that is called after reloadData.

And afterDelay:2 may be too short or too long, so how can i implement it? It actually stays the same if the offset is less than the new amount of data. I noticed that the sizing information was off after doing reloadData, so I realized I needed to force it to layout immediately before setting the content offset back. Calling reloadData on the tableView does not change the content offset. For me, I had issues in iOS 8 while using this.

It was problem with iOS 8 as there was no issue with iOS 9 devices. This is what I did. By default, reloadData keeps the contentOffset. However, it could be updated if you do have inaccurate estimatedRowHeight values. I had the same issue however none of answers suggested here worked. But sometimes problem lyes in different place.

Sometimes the problem lyes in contentInsets of table view. If you make reload data while tableView is not visible on the screen you can faced with wrong offset after table view appears on the screen. It happens because UIViewController can control insets if his scrollView then the scrollView is appearing to allow lying of scrollView below transparent navigationBar and statusBar.

Tags: uitableviewview. February 24, Ios Leave a comment. This leaves It would take 2 or more tries to make my taps register. So I decided to run my app in Simulato Add menu. This worked for me Swift 3 self. GetTokens: what is the purpose of the oldCookieToken parameter?

Why is the top portion of my UISegmentedControl not tappable?I feel like this might be a common issue and was wondering if there was any common solution to it. Basically, my UITableView has dynamic cell heights for every cell. I believe this is due to the fact that because I reloaded data, as I'm scrolling up, the UITableView is recalculating the height for each cell coming into visibility.

Further, when I do manage to scroll all the way to the top, I can scroll back down and then up, no problem with no jumping. NOTE: I actually know the size of my cells. There are only two possible values.

Understanding Reload, Repaint, and Re-Layout for UITableView

If your cells are truly variable-sized, then you might want to cache the cell. There is a bug which I believe was introduced in iOS That is when you do a reload the tableView contentOffSet gets unexpectedly altered. In fact contentOffset should not change after a reload. You have to save your contentOffSet and set it back to your saved value after your reload is finished. Try to call cell. It's known bug in iOS8. You can in fact reload only certain rows by using reloadRowsAtIndexPathsex:.

Igor answer is working fine in this case, Swift-4 code of it. I have 2 different cell heights. I ran into this today and observed: It's iOS 8 only, indeed. With this, all weird jittering and jumping around in my UITableViews has stopped. Swift 3 version of accepted answer. It tends to happen due to miscalculations of UITableViewAutomaticDimension You have to save your contentOffSet and set it back to your saved value after your reload is finished.

None But, in general, you could also animate table cell height changes like so: tableView.In this iOS Programming post, I would like to answer two common questions raised by our readers. Let us first take a look at the first question and see how to display a text message when the table is empty. The UITableView class includes the backgroundView property, which is the background view of the table view.

This property is set to nil by default. To display a message or an image when the table is empty, usually we configure this property and set it to our own view. There is no better way to illustrate the usage by an example. You can first download the sample project from here.

uitableview scroll to top after reload swift

The demo app uses a free web-based API from Kiva. Kiva provides free web-based APIs for developers to access their loan data. If you compile and run the demo app now, it shows a blank table. When there is no data available i. Please pull down to refresh. Easy, right? Now that you should have a basic understanding of the backgroundView property.

The pull-to-refresh interaction for updating data of a table view was originally created by Loren Brichter. Prior to iOS 6, adding such feature to your app is not an easy task.

You either implement your own solution or rely on third-party libraries. Both solutions will take you considerable time and efforts. Thanks to the popularity of the pull-to-refresh feature. Starting from iOS 6. With the built-in control, you just need to write a few lines of code to add the pull-to-refresh feature in your app.

The UIRefreshControl is a standard control that provides the pull-to-refresh feature. You can simply link a refresh control with a table view controller. The controller then automatically adds the control to the table and manage the display of that control in response to the appropriate gestures. It is very simple to use UIRefreshControl.

Optionally, you can change the background color and tint color by using the backgroundColor and tintColor properties respectively. Instead, when the table view is pulled down sufficiently, the refresh control triggers the UIControlEventValueChanged event. We have to assign an action method to this event and use it to perform the retrieval of the latest loans that is, calling the getLatestLoans: method. If you compile and run the app, the pull-to-refresh feature should work.

Try to pull down the table until it triggers the refresh. But there is a problem. The refresh control still appears even after the table content is loaded. To hide the control, you can simply call the endRefreshing: method:. The UIRefreshControl provides an option for you to assign a title text via the attributedTitle property. So, update the method with the following code:. The code is very straightforward.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Basics of Pull to Refresh for Swift Developers

I am facing problem in my app - you can post and edit your favorite places. My problem is: the UITableview scrolls to the top after reloading. But that's not what I want. But I don't know how to manage that. To prevent scrolling to top, you should save heights of cells when they loads and give exact value in tableView:estimatedHeightForRowAtIndexPath :. It works well, but is usually jarring and a bad user experience if you're going to do that with a tableview that the user is currently looking at.

Learn more. Asked 3 years, 10 months ago. Active 2 months ago. Viewed 18k times. Could you help me? Sohil R. Memon 8, 1 1 gold badge 26 26 silver badges 48 48 bronze badges. Active Oldest Votes. Kurt J Kurt J 2, 18 18 silver badges 17 17 bronze badges. Thank you! I was calculating row height from textview text. Small bump comes in when reloading tableview. Adding this code will remove that small bump. Igor Igor 9, 3 3 gold badges 42 42 silver badges 63 63 bronze badges.

This is working for the tableviews when doing for bottom scroll. But not working in case of top scroll. Can you give some suggestion what may be the issue exactly? Do you mean a case when you scroll to some indexPath programmatically, skipping some first rows? If yes, then you can do pre-calculate heights. SpacyRicochet SpacyRicochet 2, 1 1 gold badge 20 20 silver badges 38 38 bronze badges. In swift 3.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *