-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedInGroupTableViewController.swift
More file actions
217 lines (166 loc) · 7.54 KB
/
Copy pathSortedInGroupTableViewController.swift
File metadata and controls
217 lines (166 loc) · 7.54 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// SortedInGroupTableViewController.swift
// TableViewSortedInGroup
//
// Created by ogre on 15/9/16.
// Copyright © 2015年 ogre. All rights reserved.
//
import UIKit
class SortedInGroupTableViewController: UITableViewController {
var contextList = [String]()
var _resortedList = [String:Array<String>]()
var _keyList = Array<String>()
func initSettings(newContent:[String])
{
self.setContent(newContent)
}
func setContent(newContent:[String])
{
self.contextList = newContent
self.resortContentList()
self.tableView.reloadData()
}
// resort the Content, and make them in queue
func resortContentList()
{
for conText in contextList
{
let firstCharactor = String(conText[conText.characters.startIndex]).uppercaseString
if _resortedList.keys.contains(firstCharactor)
{
_resortedList[firstCharactor]?.append(conText)
}
else
{
_resortedList.updateValue([conText], forKey: firstCharactor)
}
}
for key in _resortedList.keys
{
_resortedList[key] = _resortedList[key]?.sort()
}
_keyList = _resortedList.keys.sort()
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return _keyList.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return String(_keyList[section])
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return _resortedList[_keyList[section]]!.count
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return self._keyList
}
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return self._keyList.indexOf(title)!
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let curSection = indexPath.section
let curRow = indexPath.row
let curString = _resortedList[_keyList[curSection]]?[curRow]
let row = contextList[contextList.indexOf(curString!)!]
cell.textLabel!.text = row
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
// deal with the editing
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
// add some actions
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
// add a delete button
let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "删除", handler: deleteRow)
deleteRowAction.backgroundColor = UIColor.redColor()
return [deleteRowAction]
}
func deleteRow(action:UITableViewRowAction, indexPath:NSIndexPath)
{
// Cancel
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
alertAction in
NSLog("Cancel")})
// OK
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {alertAction in
let curText = self._resortedList[self._keyList[indexPath.section]]?[indexPath.row]
self.contextList.removeAtIndex(self.contextList.indexOf(curText!)!)
self._resortedList[self._keyList[indexPath.section]]?.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
if self._resortedList[self._keyList[indexPath.section]]?.count == 0
{
self._keyList.removeAtIndex(indexPath.section)
self.tableView.reloadData()
}
})
let markAlertView = UIAlertController(title: "Delete", message: "Are you sure to delete it?", preferredStyle: UIAlertControllerStyle.Alert)
markAlertView.addAction(cancelAction)
markAlertView.addAction(okAction)
self.presentViewController(markAlertView, animated: true, completion: nil)
}
/*
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
*/
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
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
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}