Building a To Do List iOS Mobile App – Part 2

In the first part of the simple iOS mobile app (toDoList) tutorial series, you covered the basics of setting environment and user interface.

We’ll start where we left off last tutorial, so open your project from last time, or go through the previous tutorial first. http://intellij.my/2016/09/03/building-a-to-do-list-ios-mobile-app-part-1/

Select UITableViewController in Main.Storyboard and edit Attribute Inspector according to this setting:

Style: Basic
Identifier: TaskCell
Accessory: Disclosure Indicator

Attribute Inspector.

Attribute Inspector.

 

UiTableViewController

UiTableViewController

Go to UIViewController and make sure the user interface is similar like this one. Don’t forget to set constraints for each elements. If you don’t know how to do it, feel free to refer it here, https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/DesigningaUserInterface.html.

UIViewController

UIViewController

Create two Cocoa Touch Class. One is TableViewController.swift(UITableViewController) and the other one is ViewController.swift(UIViewController).

Xcode 7.3.1 – New File

Xcode 7.3.1 – New File

 

Cocoa Touch Class

Cocoa Touch Class

 

UITableViewController

UITableViewController

 

UIViewController

UIViewController

Then make sure you attach both files into each controllers in Identity inspector.

Identity inspector – UITableViewController

Identity inspector – UITableViewController

 

Identity inspector -UIViewController

Identity inspector -UIViewController

Open Info.plist and add these code at the end third last line of code before tag.

...
</array>
<key>NSAppTransportSecurity</key>
	<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
	</dict>
</dict>
</plist>

Open TableViewController.swift and update the with this code.

import UIKit
import Alamofire
import SwiftyJSON

class TableViewController: UITableViewController {

    var id: String!
    var name: String!
    var descriptions: String!
    var tasks:[JSON] = []
    
    @IBAction func addNewTask(sender: AnyObject) {
        self.id = nil
        self.performSegueWithIdentifier("MySegue", sender: self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData()
    }
    
    override func viewWillAppear(animated: Bool) {
        loadData()
    }
    
    func loadData() {
        Alamofire.request(.GET , "http://demo.revivalx.com/todolist-api/get_all_tasks.php", parameters: [:])
            .responseJSON { response in
                if response.result.isSuccess {
                    var jsonObj = JSON(response.result.value!)
                    if let data = jsonObj["tasks"].arrayValue as [JSON]?{
                        self.tasks = data
                        self.tableView.reloadData()
                    }
                }
        }
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tasks.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath)
        let task = tasks[indexPath.row]
        cell.textLabel?.text = task["name"].stringValue
        return cell
    }
    
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            let task = tasks[indexPath.row]
            Alamofire.request(.GET , "http://demo.revivalx.com/todolist-api/delete_task.php", parameters: ["taskId":task["taskId"].stringValue])
                .responseJSON { response in
                    if response.result.isSuccess {
                        self.loadData()
                    }
            }
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let task = tasks[indexPath.row]
        id = task["taskId"].stringValue
        name = task["name"].stringValue
        descriptions = task["description"].stringValue
        self.performSegueWithIdentifier("MySegue", sender: self)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "MySegue" {
            let viewController: ViewController = segue.destinationViewController as! ViewController
            viewController.id = id
            viewController.name = name
            viewController.descriptions = descriptions
        }
    }
}

Open ViewController.swift and replace the with this code.

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {

    @IBOutlet weak var txtName: UITextField!
    @IBOutlet weak var txtDescription: UITextField!
    var id: String!
    var name: String!
    var descriptions: String!
    var tasks:[JSON] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if id != nil {
            txtName.text = name
            txtDescription.text = descriptions
        }
    }

    @IBAction func btnSubmit(sender: AnyObject) {
        if id != nil {
            Alamofire.request(.POST , "http://demo.revivalx.com/todolist-api/update_task.php", parameters: ["taskId":id,"name":txtName.text!,"description":txtDescription.text!])
                .responseJSON { response in
                    if response.result.isSuccess {
                        self.navigationController?.popViewControllerAnimated(true)
                    }
            }
        } else {
            Alamofire.request(.POST , "http://demo.revivalx.com/todolist-api/create_task.php", parameters: ["name":txtName.text!,"description":txtDescription.text!])
                .responseJSON { response in
                    if response.result.isSuccess {
                        self.navigationController?.popViewControllerAnimated(true)
                    }
            }
        }
    }
}

One thought on “Building a To Do List iOS Mobile App – Part 2

Leave a Reply

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