When I was working on building some native iOS UI, UITableView was one of the things that I was working with and at some point during my work, I wanted to refresh the table and I was expecting a built-in method like UITableView.refresh. Naturally, one of the first things that I tried was to type in the variable name in Xcode, key in “.” and type “r” expecting the Autocomplete to show the refresh method. However to my surprise there was no refresh method? and I thought what? A basic feature like this should surely be there. Turns out that there is a way to refresh the table, it’s just not immediately obvious to someone who’s new to iOS. So in this post, I will talk about my solution to refresh table. This post is a tutorial on how to refresh a table in iOS Swift i.e. how to refresh a UITableView in Swift using the refresh method.

Problem

Refresh UITableView: the tableView in question only has only one section and it should be refreshed with an animation by clicking a button or otherwise.  Provide a general, reusable method to refresh UITableView.

Refresh a UITableView in Swift

The solution uses one of the UITableView methods combined with the awesome new concept of extensions.

Test Canvas

For the purpose of this post, I have created an Xcode project which shows a table with 3 rows and Refresh button on top of the table,  pushing which will trigger a table refresh. It is a simple project and you can download the source code from Github.

Add the refresh extension

A detailed explanation of extensions in Swift is beyond the scope of this post so if you would like to know more, then have a a read of this. Here we use the extension to add the refresh method to UITableView so all instances of UITableView in our app have a refresh method. Here’s the code for our extension, p.s. we cannot simply use the reloadData method since we want the refresh table accompanied by an animation
extension UITableView {
    func refreshTable(){
        let indexPathForSection = NSIndexSet(index: 0)
        self.reloadSections(indexPathForSection, withRowAnimation: UITableViewRowAnimation.Middle)
    }
}
We are simply using the reloadSections of method of UITableView with one of the UITableViewRowAnimation. Since we only have one section in our table, we are just reloading the section at index 0 i.e. 1. You can download the source code for the entire project on here. You can have a look at all the code in the ViewController for that project. You notice that the project is
  1. Single View Application
  2. with a UITableView
  3. populated with some simple data i.e var data = [“One”, “two”, “three”]
  4. a button labeled, Refresh
  5. and contains the following code for  the button’s TouchUpInside event
@IBAction func reloadTable(sender: AnyObject) {
    data = ["four","five","six"]
    origData = data
    tableView.refreshTable()
}
I have kept this post simple just to set the foundation for the next post where I will be using this logic to show how we can achieve simple pagination for UITableView.

Get updates?

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store.  https://mydaytodo.com/apps/ Also, if you can leave a review on the App Store or Google Play Store, that would help too.
 
Categories: iOSSwift

0 Comments

Leave a Reply

Avatar placeholder
Verified by MonsterInsights