Swift Table View Reload Creating Cell Instance Again

Today's topic we will hash out the Pull to Refresh in Swift, or you tin say UIRefresh Control.

UIRefersh Control inherits from UIControl. In improver, UIControl inherits from the UIView and  UIView inherits from the UIResponder.

Finally, UIResponder inherits from the NSObject class.

Nosotros can use UIRefresh Control with whatever UIScrollView objects like UITableview or UICollectionView.

We will bear witness how to use UIRefresh Control when nosotros pull downward the UIScrollView object and refresh its content.

Pull to Refresh in Swift

UI Implementation for Pull to Refresh in Swift:

We are going to implement the Pull to Refresh functionality by calling an API and populating the table view with the data returned from the API.

one. Let'due south begin by creating an Xcode project, adding a tabular array view and a label in the Main storyboard.

2. Create TableViewCell and Xib. Later that, annals TableViewCell and assign consul and dataSource.

iii. Table view cell is populated with the information returned by the API call

Pull to Refresh in Swift

Calculation basic functionality for Pull to Refresh in Swift:

i. Create a model to parse the data equally beneath. Afterward that, we volition decode the API response using JSONDecoder.

2. Till at present, your viewDidLoad() function would look like beneath.

3.  Create a variable array to store the desired values. For instance, we accept created var apiInformation = [String]()

4. After that, we will create a office fetchAPIData() for the network-related task. This part is chosen from the viewDidLoad().

five. In the above lawmaking nosotros are hitting the API and decoding the returned information to DataModel DataResponse using Codable protocol.

6. We are then appending the data to apiData array of type String. We will now populate our TableView with the data of the apiData array, shown beneath.

Calculation UIRefreshControl

Till at present we prepared our ViewController course to use the UIRefreshControl which will be visible when nosotros pull down our Tabular array view.

UIRefreshControl will start working when the user pulls the Tableview, it will call the fetchAPIData() function and reloads the tabular array view with new data returned from the API.

refreshControl will telephone call the objective-C office callPullToRefresh based on its value changes through the listener .valueChanged

Later that, add the below code to your viewDidLoad()

Now declare an objective-C function which will call the fetchAPIData() function.

In addition to the above, we must update our UI in the main thread. Therefore, use the beneath code later on the network call in fetchAPIData() function.

endRefreshing() function tells the condition of the UIRefreshControl that it has finished refreshing the UITableView. Similarly, beginRefreshing() tells the condition of the UIControl is now started refreshing the contents.

In the higher up code, nosotros have made a filibuster of 5 seconds for refreshControl. The refreshControl volition be visible for five seconds.

Accordingly, we are hiding and unhiding the refreshLabel.

We are updating the refreshLabel in the below code within the fetchAPIData()

isRefreshing is a boolean belongings that describes the current country of  the UIRefreshControl

Based on the isRefreshing value we are updating the refreshLabel status.

Now your fetchAPIData() part would like as shown in the beneath image.

pull to refresh in swift

Result

After successfully adding the UIRefreshControl, your app volition wait similar below.

Conclusion

Here, we take used the custom refreshLabel to show the refreshing status of the UIRefreshControl notwithstanding, yous tin utilise the UIRefreshControl aspect attributedTitle for showing the status of the UIRefreshControl.

Yous can likewise bank check the Apple documentation for UIRefreshControl here

We hope this blog will help you in creating interactive designs for your projects.

For other blogs Please click here

. . .

pyleearelleth.blogspot.com

Source: https://mobikul.com/pull-to-refresh-in-swift/

0 Response to "Swift Table View Reload Creating Cell Instance Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel