我有一个表视图,该视图将日历事件动态加载到行中。我正在检查设备的当日活动。我设置了一个detailview,因为我会将事件数据传递到detailview标签。我正在使用情节提要剧集。但是我还不能完成。有人可以帮我我所缺少的吗?
错误是“无法分配'EKEvent类型的值?'键入“字符串?”” 我应该将EKEvent Date类型转换为字符串吗?或如何,我需要一个例子。我正在练习快速技能。
下面是我的代码;
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var needPermissionView: UIView!
@IBOutlet weak var calendarsTableView: UITableView!
@IBOutlet weak var dateLabel: UILabel!
var event : [EKEvent]?
var calendars: [EKCalendar]?
let eventStore = EKEventStore()
override func viewDidLoad() {
super.viewDidLoad()
loadEvents()
calendarsTableView.delegate = self
calendarsTableView.dataSource = self
calendarsTableView.estimatedRowHeight = 44
calendarsTableView.rowHeight = UITableViewAutomaticDimension
calendarsTableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
checkCalendarAuthorizationStatus()
}
func checkCalendarAuthorizationStatus() {
let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
switch (status) {
case .notDetermined:
requestAccessToCalendar()
case .restricted, .denied:
needPermissionView.fadeIn()
case .authorized:
loadCalendars()
loadEvents()
refreshTableView()
}
}
func requestAccessToCalendar() {
eventStore.requestAccess(to: .event) { (accessGranted, error) in
if accessGranted == true {
DispatchQueue.main.async {
self.loadCalendars()
self.loadEvents()
self.refreshTableView()
}
} else {
DispatchQueue.main.async {
self.needPermissionView.fadeIn()
}
}
}
}
func loadCalendars() {
self.calendars = eventStore.calendars(for: EKEntityType.event)
}
func refreshTableView() {
calendarsTableView.isHidden = false
calendarsTableView.reloadData()
}
func loadEvents(){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = Date()
let dateString = dateFormatter.string(from: date)
print(dateString)
dateLabel.text = dateString
var components: DateComponents = DateComponents()
components.setValue(1, for: .day)
let expirationDate = Calendar.current.date(byAdding: components, to: date)
//print(expirationDate ?? "expirationDate")
let endString = dateFormatter.string(from: expirationDate!)
// Create start and end date NSDate instances to build a predicate for which events to select
let startDate = dateFormatter.date(from: dateString)
let endDate = dateFormatter.date(from: endString)
if let startDate = startDate, let endDate = endDate{
let eventStore = EKEventStore()
// Use an event store instance to create and properly configure an NSPredicate
let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)
// Use the configured NSPredicate to find and return events in the store that match
self.event = eventStore.events(matching: eventsPredicate).sorted(by: { (e1, e2) -> Bool in
return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending
})
}
}
@IBAction func goToSettingsButtonTapped(_ sender: UIButton) {
let openSettingsUrl = URL(string: UIApplicationOpenSettingsURLString)
UIApplication.shared.openURL(openSettingsUrl!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let events = self.event {
return events.count
}
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 90
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if let events = self.event {
let eventName = events[(indexPath as NSIndexPath).row].title
cell.textLabel?.text = eventName
let eventDate = events[(indexPath as NSIndexPath).row].startDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3")
let timeStamp = dateFormatter.string(from: eventDate!)
print(timeStamp)
cell.detailTextLabel?.text = timeStamp
} else {
cell.textLabel?.text = "Unknown Calendar Name"
}
cell.textLabel?.numberOfLines = 0
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = calendarsTableView.indexPathForSelectedRow {
let destVC = segue.destination as! DetailViewController
destVC.taskNameLabel.text = event?[indexPath.row]
}
}
}
答案 0 :(得分:1)
您的问题在这里EKEvent
var无法分配给类型为text
的{{1}}
String
您需要
destVC.taskNameLabel.text = event?[indexPath.row]
//
destVC.sendedEvent = event?[indexPath.row]
然后分配class DetailViewController {
var sendedEvent: EKEvent?
}
的标题或sendedEvent
的{{1}}内的任何内容