Skip to content

Commit 9bd08db

Browse files
committed
Added sections to UITableView grouped by date message received.
1 parent 02254c8 commit 9bd08db

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Swift Push/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>272</string>
22+
<string>283</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push/PushMessages.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ import CoreData
1111

1212

1313
class PushMessages: NSManagedObject {
14-
15-
// Insert code here to add functionality to your managed object subclass
16-
14+
// Insert code here to add functionality to your managed object subclass
15+
16+
var sectionCriteria: String {
17+
get {
18+
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
19+
let dayReceived = cal.dateFromComponents(cal.components([.Day , .Month, .Year ], fromDate: timeReceived!))
20+
let dayToday = cal.dateFromComponents(cal.components([.Day , .Month, .Year ], fromDate: NSDate()))
21+
22+
if (dayReceived == dayToday) {
23+
return "Today"
24+
} else {
25+
return NSDateFormatter.localizedStringFromDate(dayReceived!, dateStyle: .MediumStyle, timeStyle: .NoStyle) as String
26+
}
27+
}
28+
}
1729
}

Swift Push/ViewController.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
2929
fetchRequest.sortDescriptors = [sortDescriptor]
3030

3131
// Initialize Fetched Results Controller
32-
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
32+
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "sectionCriteria", cacheName: nil)
3333

3434
// Configure Fetched Results Controller
3535
fetchedResultsController.delegate = self
@@ -59,16 +59,22 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
5959
if let sections = fetchedResultsController.sections {
6060
return sections.count
6161
}
62-
6362
return 0
6463
}
6564

65+
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
66+
if let sections = fetchedResultsController.sections {
67+
let currentSection = sections[section]
68+
return currentSection.name
69+
}
70+
return nil
71+
}
72+
6673
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
6774
if let sections = fetchedResultsController.sections {
6875
let sectionInfo = sections[section]
6976
return sectionInfo.numberOfObjects
7077
}
71-
7278
return 0
7379
}
7480

@@ -77,7 +83,6 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
7783

7884
// Configure Table View Cell
7985
configureCell(cell, atIndexPath: indexPath)
80-
8186
return cell
8287
}
8388

0 commit comments

Comments
 (0)