我有两个View控制器。两者都有TableViews。主要的一个很好。第二个(detailView)在此处显示第一个控制器的行(单元格)的详细信息。它甚至在显示选择器错误之前就崩溃了,我相信,如果数据源未正确连接,虽然我相信是这样,但是这会发生。
我试图取出tableView填充,只是为了确保第二个视图控制器将显示,并且确实如此。 我尝试在情节提要中删除并重新创建指向视图表的委托和数据源链接。
Detail View Controller代码。
import UIKit
import EventKit
class DetailViewController: UIViewController, UITableViewDelegate {
@IBOutlet var detailView: UITableView!
@IBOutlet weak var textTitle: UITextField!
var pText: String? // for passing data in from main view
private var items: [String] = []
var calendarEvents: [EKEvent]?
var myCalendar: EKCalendar?
override func viewDidLoad() {
super.viewDidLoad()
//detailView.delegate = self // detailView is nil
//detailView.dataSource = self
textTitle.text = pText
loadItems()
}
let eventStore = EKEventStore()
@IBAction func goBack(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func loadItems() {
let cal = Calendar.current // the generic calendar, nothing to do with calendar passed in
var oneDayAgoComponents = DateComponents()
oneDayAgoComponents.day = -1
let oneDayAgo = cal.date(byAdding: oneDayAgoComponents, to: Date())
var futureComponents = DateComponents()
futureComponents.day = 30
let futureDate = cal.date(byAdding: futureComponents, to: Date())
let dateFmt = DateFormatter()
dateFmt.dateFormat = "dd-MMM-yyyy"
dateFmt.timeZone = TimeZone.current
var predicate: NSPredicate? = nil
if let anAgo = oneDayAgo, let aFuture = futureDate, let aCal = myCalendar { // unwrapping
predicate = eventStore.predicateForEvents(withStart: anAgo, end: aFuture, calendars: [aCal])
if let myPred = predicate {
self.calendarEvents = eventStore.events(matching: myPred)
if let calEvents = calendarEvents {
for cEvent in calEvents {
let date = dateFmt.string(from: cEvent.startDate)
print (cEvent.title as Any, date)
items.append(cEvent.title + ", " + date)
}
}
}
}
}
}
extension DetailViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dtlcell")!
let text = items[indexPath.row]
cell.textLabel?.text = text
return cell
}
}
错误如下:
-[UIView tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x7fa168e0fcd0。