-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventsViewController.swift
63 lines (50 loc) · 2.08 KB
/
EventsViewController.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
//
// EventsViewController.swift
//
//
// Created by Sophie Hsu on 11/05/2017.
//
//
import UIKit
import EventKit
class EventsViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var calendar: EKCalendar!
var events: [EKEvent]?
override func viewDidLoad() {
super.viewDidLoad()
loadEvents()
}
func loadEvents() {
// Create a date formatter instance to use for converting a string to a date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// Create start and end date NSDate instances to build a predicate for which events to select
let startDate = dateFormatter.date(from: "2016-01-01")
let endDate = dateFormatter.date(from: "2016-12-31")
if let startDate = startDate, let endDate = endDate {
let eventStore = EKEventStore()
// Use an event store instance to create and properly configure an NSPredicate
let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: [calendar])
// Use the configured NSPredicate to find and return events in the store that match
self.events = eventStore.events(matching: eventsPredicate).sorted(){
(e1: EKEvent, e2: EKEvent) -> Bool in
return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let events = events {
return events.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell")!
cell.textLabel?.text = events?[(indexPath as NSIndexPath).row].title
return cell
}
}