Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForRowAtIndexPath never called in UITableViewController

cellForRowAtIndexPath is never called (numberOfRowsInSection is) using UITableViewController with the following way:

import UIKit
import EventKit

class EventThisWeekController: UITableViewController {

    var eventStore: EKEventStore = EKEventStore()
    var eventsThisWeek: [EKEvent] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.eventStore.requestAccessToEntityType(.Event) {
            (granted, error) -> Void in

            guard granted else { fatalError("Permission denied") }

            let endDate = NSDate(timeIntervalSinceNow: 604800)
            let predicate = self.eventStore.predicateForEventsWithStartDate(NSDate(),
                                                                            endDate: endDate,
                                                                            calendars: nil)
            self.eventsThisWeek = self.eventStore.eventsMatchingPredicate(predicate)
            print(self.eventsThisWeek.count)
        }

    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        print("cellForRowAtIndexPath")
        let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)

        cell.textLabel?.text = self.eventsThisWeek[indexPath.row].title

        return cell
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("numberOfRowsInSection")
        return eventsThisWeek.count
    }
}

Data source and delegate is assigned to this Controller and EvenThisWeekController is a class designed in main storyboard. As result, in my app table is displayed without any result. Of course, eventThisWeek array length is not equal to 0. Any ideas how I can solve it?

like image 247
Juan Garcia Avatar asked Dec 14 '25 02:12

Juan Garcia


1 Answers

Replace your code in viewDidLoad with this.

self.eventStore.requestAccessToEntityType(.Event) {
    (granted, error) -> Void in

    guard granted else { fatalError("Permission denied") }

    let endDate = NSDate(timeIntervalSinceNow: 604800)
    let predicate = self.eventStore.predicateForEventsWithStartDate(NSDate(),
                                                                    endDate: endDate,
                                                                    calendars: nil)
    self.eventsThisWeek = self.eventStore.eventsMatchingPredicate(predicate)
    dispatch_async(dispatch_get_main_queue(),{

        self.tableView.reloadData()

    })
    print(self.eventsThisWeek.count)
 }

Your cellForRowAtIndexPath doesn't call because when your view is loaded at that time array was empty and when the self.eventStore.requestAccessToEntityType is completely executed and load array with new value you weren't notifying your tableView to reload the data as you have new values in your array.

like image 177
Syed Qamar Abbas Avatar answered Dec 15 '25 18:12

Syed Qamar Abbas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!