问题描述
cellForRowAtIndexPath(numberOfRowsInSection):
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
}
}
数据源和委托已分配给此Controller,EvenThisWeekController是在主故事板上设计的类.结果,在我的应用程序表中显示没有任何结果.当然,eventThisWeek数组的长度不等于0.有什么想法可以解决吗?
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?
推荐答案
以此替换您在viewDidLoad中的代码.
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)
}
您的 cellForRowAtIndexPath
不会调用,因为当您的视图在那个时间加载时,数组为空,并且self.eventStore.requestAccessToEntityType完全执行,并且以新值加载数组时,您没有通知您的tableView可以在数组中有新值时重新加载数据.
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.
这篇关于从未在UITableViewController中调用cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!