-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesCollectionViewController.swift
More file actions
187 lines (156 loc) · 6.76 KB
/
NotesCollectionViewController.swift
File metadata and controls
187 lines (156 loc) · 6.76 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
//
// NotesCollectionViewController.swift
// NoteSwap
//
// Created by Nick Dugal on 4/17/16.
// Copyright © 2016 Nick Dugal. All rights reserved.
//
import UIKit
import Parse
private let sectionInsets = UIEdgeInsets(top: 50.0, left: 20.0, bottom: 50.0, right: 20.0)
class NotesCollectionViewController: UICollectionViewController {
private let reuseIdentifier = "NoteCell"
@IBOutlet var resultsCollectionView: UICollectionView!
var classNum: String!
var semester: String!
var professor: String!
var year: String!
var course: String!
// var searchString: String = " "
var resultsArray: NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
resultsCollectionView.delegate = self
resultsCollectionView.dataSource = self
self.findNotes()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(NotesCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
func findNotes() {
// resultsArray.removeAllObjects()
let findIt: PFQuery = PFQuery(className: courseGlobal)
// searchString = course
if (classNum != "") {
findIt.whereKey("Class", equalTo: classGlobal)
// searchString += " \(classNum)"
}
if (semester != "") {
findIt.whereKey("Semester", equalTo: semesterGlobal)
// searchString += " \(semester)"
}
if (professor != "") {
findIt.whereKey("Professor", equalTo: professorGlobal)
// searchString += " \(professor)"
}
if (year != "") {
findIt.whereKey("Year", equalTo: yearGlobal)
// searchString += " \(year)"
}
findIt.findObjectsInBackgroundWithBlock { (objects, error) in
self.resultsCollectionView.reloadData()
if (error != nil) {
print(error?.description)
}
else {
if (objects?.count == 0) {
self.resultsCollectionView.hidden = true
//self.noResults.hidden = false
}
else {
for object in objects! {
if let objFile = object["Note"] as? PFFile {
objFile.getDataInBackgroundWithBlock({ (objData, error2) in
self.resultsCollectionView.reloadData()
if (error == nil) {
self.resultsArray.addObject(objData!)
} else {
print(error2?.description)
}
})
}
}
}
}
}
// self.layoutSetup()
resultsCollectionView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// 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.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return resultsArray.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NotesCollectionViewCell
//Configure the Cell
if (resultsArray.count > 0) {
let imData = resultsArray[indexPath.row]
let image = UIImage(data: imData as! NSData)
cell.imageView.image = image
}
return cell
}
//Allows adjustment of the pic size inside a collection view
func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let imData = resultsArray[indexPath.row]
let image = UIImage(data: imData as! NSData)
return image!.size
}
}
extension NotesCollectionViewController : UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
// 1
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
textField.addSubview(activityIndicator)
activityIndicator.frame = textField.bounds
activityIndicator.startAnimating()
self.findNotes()
activityIndicator.stopAnimating()
// print("Found \(resultsArray.count) matching \(searchString)")
self.resultsCollectionView.reloadData()
textField.text = nil
textField.resignFirstResponder()
return true
}
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}
override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
}
*/