ios - cellForRowAtIndexPath never called in UITableViewController -
cellforrowatindexpath never called (numberofrowsinsection is) using uitableviewcontroller 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 , delegate assigned controller , eventhisweekcontroller class designed in main storyboard. result, in app table displayed without result. of course, eventthisweek array length not equal 0. ideas how can solve it?
replace code in viewdidload 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 view loaded @ time array empty , when self.eventstore.requestaccesstoentitytype executed , load array new value weren't notifying tableview reload data have new values in array.
Comments
Post a Comment