从未使用UITableViewController通过以下方式调用cellForRowAtIndexPath(numberOfRowsInSection是):
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
}
}
数据源和委托已分配给此Controller,EvenThisWeekController是在主故事板上设计的类。结果,在我的应用程序表中显示没有任何结果。当然,eventThisWeek数组的长度不等于0。有什么想法可以解决吗?
最佳答案
以此替换您在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)
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
})
print(self.eventsThisWeek.count)
}
之所以不会调用
cellForRowAtIndexPath
,是因为当时在视图加载时数组为空,并且self.eventStore.requestAccessToEntityType完全执行并且使用新值加载数组时,您并没有通知tableView重新加载新的数据数组中的值。关于ios - 从未在UITableViewController中调用cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40442440/