我正在尝试在 tableViewCell 的核心数据中共享我保存的事件,但是每当我按下共享时,它都会显示 activityViewController 但不会给我任何共享它的选项。我也尝试在我的 iPhone 上运行它,并且出现了同样的问题。
class EventsTableViewController: UITableViewController {
@IBOutlet var table: UITableView!
var eventsArray: [NSManagedObject] = []
// The Managed Object Context retrieved from the app delegate
let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem
}
override func viewWillAppear(_ animated: Bool) {
gettAllRecords()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return eventsArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
let event = eventsArray[indexPath.row]
let eventTitle = event.value(forKeyPath: "eTitle") as? String
let eventLocation = event.value(forKeyPath: "eLocation") as? String
let eventDateTime = event.value(forKeyPath: "eDateTime") as? String
cell.titleLable.text = eventTitle
cell.locationLable.text = eventLocation
cell.dateTimeLable.text = eventDateTime
return cell
}
/***********************************************************************
*
* This function gets all records from the database and returns
* an array of ManagedObject
*
**********************************************************************/
func gettAllRecords() {
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Event")
do {
eventsArray = try managedContext.fetch(fetchRequest)
table.reloadData()
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let shareActions = UITableViewRowAction(style: .normal, title: "Share") { (_ rowAction: UITableViewRowAction, _ indexPath: IndexPath) in
let shareEvent = self.eventsArray[indexPath.row] //I feel like shareEvent is not being populated with self.eventsArray
let activityViewController = UIActivityViewController(activityItems: [shareEvent], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
let deleteAction = UITableViewRowAction(style: .default, title: "Delete") { (_ rowAction: UITableViewRowAction, _ indexPath: IndexPath) in
let event = self.eventsArray[indexPath.row]
self.managedContext.delete(event)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
self.gettAllRecords()
}
shareActions.backgroundColor = UIColor.gray
return [deleteAction, shareActions]
}
知道这里发生了什么吗?我也尝试在我的 iPhone 上运行它,并且出现了同样的问题。