Gold Challenge (simple way) - Chapter 27


#1

import UIKit

class ViewController: UIViewController, UITableViewDelegate { // “UITableViewDelegate”: my add for the Gold Challenge

@IBOutlet var itemTextField: UITextField!

@IBOutlet var tableView: UITableView!

let todoList = TodoList()

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.dataSource = todoList
    tableView.delegate = self                                                            // my add for the Gold Challenge
}

@IBAction func addButtonPressed(sender: UIButton) {
    guard let text = itemTextField.text where text != "" else {
        return
    }
    todoList.addItem(text)
    tableView.reloadData()
    itemTextField.text = ""
}

// my add for the Gold Challenge:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    todoList.removeItem(indexPath.row)                                                           
    tableView.reloadData()// or: "tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)"
}

}

import UIKit

class TodoList: NSObject {

private let fileURL: NSURL = {
    let documentDirectoryURLs = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    
    let documentDirectoryURL = documentDirectoryURLs.first!
    
    return documentDirectoryURL.URLByAppendingPathComponent("todolist.items")
}()

private var items: [String] = []

override init() {
    super.init()
    loadItems()
}

func saveItems() {
    let itemsArray = items as NSArray
    print("Saving items to \(fileURL)")
    if !itemsArray.writeToURL(fileURL, atomically: true) {
        print("Could not save to-do list")
    }
}

func loadItems()  {
    if let itemsArray = NSArray(contentsOfURL: fileURL) as? [String] {
        items = itemsArray
    }
}

func addItem(item: String) {
    items.append(item)
    saveItems()
}

func removeItem(index: Int) {                           // add for the Gold Challenge                                                                    
    items.removeAtIndex(index)
    saveItems()
}

}

extension TodoList: UITableViewDataSource {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let item = items[indexPath.row]
    cell.textLabel!.text = item
    return cell
}
/*
// my add for the Gold Challenge (a second way to accomplish a similar task):
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        removeItem(indexPath.row)                                                                   
        tableView.reloadData()       // or: "tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)"
    }
}
*/

}