How to add extensions to iOS apps (part 3)

iOS Development, Tutorials

Welcome to the third part of our guide: "how to add extensions to iOS apps", where we’ll complete the table implementation in the main view of our iOS application.

In the previous part of our iPhone app development guide I explained how to:

  • enable resource sharing in an app
  • create a manager for saving and reading data

Table implementation

Let’s go back to our main ViewController and finish implementing the table.
We already have a place to save our data and thanks to the manager we’ve created, data can now be added and removed within the view.

First off, implement these two protocols that will allow us to use our table:
UITableViewDelegate and UITableViewDataSource.

Our class should look like this:


import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

We should see an error message, what means that we have to add the missing delegates numberOfRowslnSection and callForRowAtlndexPath. Let’s do this!
First, add the delegates service and data source to our table. In viewDidLoad() insert the following lines:


self.tableView.delegate = self
self.tableView.dataSource = self

Next, implement the required delegates.


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return LocalStorageManager.sharedInstance.readData().count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel!.text = LocalStorageManager.sharedInstance.readData()[indexPath.row]
        return cell
    }

In the first one, determine the amount of rows per section (in this case we have only one section). In the next one, define the behavior of the cell row. The only thing left is to set the text using the indexPath.row for returned table from our manager’s readData() method.

If we would run our app now, we would only see lines separating particular table rows. Add manually a few entries just to see if everything works as expected.

In the viewDidLoad() method, add three lines which will insert data to our container using our manager.


override func viewDidLoad() {
    super.viewDidLoad()
    LocalStorageManager.sharedInstance.insertData("Entry 1")
    LocalStorageManager.sharedInstance.insertData("Entry 1")
    LocalStorageManager.sharedInstance.insertData("Entry 1")
    self.tableView.delegate = self
    self.tableview.dataSource = self
}

If we run the app now, we’ll see three manually added positions in our table.

How to add extensions to iOS apps

The table with sample entries

As you can see, our entries were added to the table so everything should work properly. Let’s replace these three lines in viewDidLoad() with one that will clear all saved data.


override func viewDidLoad() {
    super.viewDidLoad()
    LocalStorageManager.sharedInstance.clearData()
    self.tableView.delegate = self
    self.tableview.dataSource = self
}

After running the app, our table should be empty. By the way, we should add the possibility to delete entries in the table through cell swiping.
Implement two required delegates:


    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let remove = UITableViewRowAction(style: .Destructive, title: "Remove") { action, index in
            LocalStorageManager.sharedInstance.removeData(index.row)
            self.tableView.reloadData()
        }     
        return [remove]
    }

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

In the first one, add a RowAction with the title Remove.
Then delete entries from our database. Remember to refresh the table with reloadData() after deleting a line. In the second delegate, define which row should have the option of choosing an action, return “true” for all of them. Now, after running the iOS app, it will be possible to delete entries by swiping a line.

iOS app development

Removing entries


Next on “How to add extensions to iOS apps”:

In the last part of our iPhone app development guide we’ll cover adding and saving files.

 


You can have a look at the finished app in our GitHub repo here.


 

Paweł Kuźniak Technical Business Analyst, IT Operations

Paweł is technical analyst, developer and IT operations specialist at Zaven. He can solve a 3x3x3 Rubik's cube in a matter of seconds and you can find more complicated ones lying around on his desk (solved, obviously). His problem-solving attitude is reflected in his work, as he likes to keep himself busy with non-trivial puzzles.

Popular posts

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.

Read more
Fears surrounding external support. How to address concerns about outsourcing software development?

Fears surrounding external support. How to address concerns about outsourcing software development?

Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.

Read more
What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?

Read more
Mobile Apps

Get your mobile app in 3 easy steps!

1

Spec out

with the help of our
business analyst

2

Develop

design, implement
and test, repeat!

3

Publish

get your app out
to the stores


back to top