-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewListViewController.swift
73 lines (53 loc) · 2.23 KB
/
NewListViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// NewListViewController.swift
// To Do List Project 1
//
// Created by Richel Cuyler on 11/16/16.
// Copyright © 2016 Richel Cuyler. All rights reserved.
//
import UIKit
class NewListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var NewListTableView: UITableView!
@IBOutlet weak var NewListItem: UITextField!
var list: List!
@IBAction func NewItemInList(_ sender: UIButton) {
let newListItem = Task(taskName: NewListItem.text!, taskDescription: "")
list.tasks.append(newListItem)
NewListItem.resignFirstResponder()
NewListItem.text = ""
NewListTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let itemCell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! NewListTableViewCell
let myRow = indexPath.row
let nameOfNewItem = list.tasks[myRow]
itemCell.newListItemCellLabel.text = nameOfNewItem.taskName
if indexPath.row % 2 == 0 {
itemCell.backgroundColor = UIColor.lightGray
}
return itemCell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
list.tasks.remove(at: indexPath.row)
NewListTableView.reloadData()
}
}
var currentListItem: Task?
override func viewDidLoad() {
super.viewDidLoad()
guard let item = currentListItem else { return }
NewListItem.text = item.taskName
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "newItemCellToListDetail" {
let itemDetailViewController = segue.destination as! ListDetailViewController
let index = NewListTableView.indexPathForSelectedRow?.row
let listItem = list.tasks[index!]
itemDetailViewController.currentItem = listItem
}
}
}