-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEditableTVC.swift
121 lines (101 loc) · 3.48 KB
/
EditableTVC.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// EditableTVC.swift
// EditableTableViewController-Swift
//
// Created by Diego on 12/25/14.
// Copyright (c) 2014 Diego.
//
import UIKit
class EditableTVC: UITableViewController {
var myItems:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = self.editButtonItem()
for i in 0 ... 4
{
myItems.addObject(String(format: "Hey %d", i))
}
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.editing)
{
return myItems.count + 1
}
else
{
return myItems.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("EditableCellPrototype", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
if(self.editing && indexPath.row == myItems.count)
{
cell.textLabel?.text = "Add ..."
}
else
{
cell.textLabel?.text = myItems[indexPath.row] as? String
}
return cell
}
// // Override to support conditional editing of the table view.
// override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// // Return NO if you do not want the specified item to be editable.
// return true
// }
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if (indexPath.row == myItems.count)
{
return UITableViewCellEditingStyle.Insert
}
else
{
return UITableViewCellEditingStyle.Delete
}
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
myItems.removeObjectAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
myItems.insertObject(String(format: "Added %ld", indexPath.row), atIndex: indexPath.row)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
if (toIndexPath.row == myItems.count) //we only check the toIndexPath because we made the AddCell not to respond to move events
{
var tmp = myItems[fromIndexPath.row] as String
myItems.removeObjectAtIndex(fromIndexPath.row)
myItems.insertObject(tmp, atIndex: toIndexPath.row-1) //to keep it in valid range for the NSMutableArray
self.tableView.reloadData()
}
else
{
var tmp = myItems[fromIndexPath.row] as String
myItems.removeObjectAtIndex(fromIndexPath.row)
myItems.insertObject(tmp, atIndex: toIndexPath.row)
}
}
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
if (indexPath.row == myItems.count)
{
return false
}
else
{
return true
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
}
}