Search code examples
iosswiftuitablevieweventsekevent

EKEvents not populating tableview in swift


I’m new to coding and swift. I’m trying to make a simple appointment app to help with my medical planning. This is my first attempt at a stack overflow question :s, I’m coding in Swift. I’ve managed to get the app access to the Event store and I’ve been able to print all of the events from all calendars into the console, but I can’t get the events to show in my table. I’ve tried to find if anyone else has had this problem on here but I still haven't been able to find a solution that worked in my project. Sorry if this has been asked else where.

I’ve added my code that I have below. I’ve checked all of my ViewController connections and made sure the TableViewCell had an identifier “cell” as well as checking the delegate and datasource connections were attached.

In the function tableView, numberOfRowsInSection, my return self.events.count is returning 0 when I print it to the console. Also my cellForRowAtIndexPath doesn't appear to be called..would this be why nothing is showing in the table? I’m a complete rookie at this so I hope some of this makes sense, I could be completely off the ball…

I really appreciate any help and guidance! Thank you!

 class AppointViewController:  UIViewController, UITableViewDataSource, UITableViewDelegate {

var eventStore: EKEventStore!
var events = [EKEvent]()
var startDate = NSDate()
var endDate = NSDate()
var cellIdentifier = "cell"

@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.automaticallyAdjustsScrollViewInsets = false

    isAppAlreadyLaunchedOnce()

    self.tableView.dataSource = self
    self.tableView.delegate = self

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier);
    self.tableView.reloadData();

}

override func viewWillAppear(animated: Bool) {
    // Fetch all events
    // Connect to the Event Store
    self.eventStore = EKEventStore()
    self.events = [EKEvent]()
    self.eventStore.requestAccessToEntityType(EKEntityType.Event) { (granted: Bool, error: NSError? ) -> Void in

        if granted {

            print("Access Granted")

            let fourYearsAgo = NSDate(timeIntervalSinceNow: -1 * 60 * 60 * 24 * 365 * 2)
            let predicate = self.eventStore.predicateForEventsWithStartDate(fourYearsAgo, endDate: self.endDate, calendars: nil)
            let events = NSMutableArray(array: self.eventStore.eventsMatchingPredicate(predicate))

            //TEST TO MAKE SURE EVENTS ARE BEING EXTRACTED
            print(events, "printing events")

            dispatch_async(dispatch_get_main_queue()) {
                dispatch_async(dispatch_get_global_queue(0, 0))  {
                    self.eventStore.enumerateEventsMatchingPredicate(predicate) {
                        (events:EKEvent, stop:UnsafeMutablePointer<ObjCBool>) in
                        if events.title.rangeOfString("events") != nil {
                            print("String not nil")
                            stop .memory = true
                        }
                    }
                }
            }
        } else {
            print("The app is not permitted to access events")
        }
    }
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


//MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //TEST PRINT EVENT COUNT
    print("event count (print) \(events.count)")

    return events.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "cell"
    let cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
    let events:EKEvent! = self.events[indexPath.row]
    cell.textLabel!.text = events.title
    cell.detailTextLabel!.text = events.startDate.description
    print("event cell returned")
    return cell
}
}

Solution

  • You didn't call:

    self.events = events
    self.tableView.reloadData()
    

    After you get the events from store. You should call this on main queue.