我正在尝试从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上运行它,并出现同样的问题。
答案 0 :(得分:0)
我同意您自己的评估,即shareEvents可能为零。 您是否在分配的行上添加了断点?在Xcode中,停在以下行:
let shareEvent = self.eventsArray[indexPath.row] //I feel like shareEvent is not being populated with self.eventsArray
并确保shareEvents不是nil。
答案 1 :(得分:0)
我不确定您的ShareEvent是什么样的,但您需要以其他应用程序支持的格式共享数据。将数据放入文件或将其转换为已知数据类型。
在这里查看一些已知的系统类型。
答案 2 :(得分:0)
您要提供分享的内容只是您的NSManagedObject
个实例。没有任何共享服务知道该怎么做,所以没有一个出现。
您可以将项目分享为图像,文本或各种标准文件类型。您需要做的是实现将NSManagedObject
数据转换为某种标准格式的内容。
创建一个采用UIActivityItemSource
协议的类。该类将获取您的数据,将其转换为一些标准格式以共享(文本,图像或适合您的应用程序的任何内容)并返回该数据。然后,不是将原始对象传递给UIActivityViewController
,而是传递UIActivityItemSource
对象。
例如,假设您的商品应作为文字分享。您将实现一个采用UIActivityItemSource
协议的类,它将包含一个获取数据并创建格式良好的字符串的函数。
class MyItemSource: UIActivityItemSource {
func activityViewController(_ activityViewController: UIActivityViewController,
itemForActivityType activityType: UIActivityType) -> Any? {
// Take your data and create a string
var stringToShare = "Hello world!"
...
return stringToShare
}
// other UIActivityItemSource methods
}
如果您将MyItemSource的实例传递给UIActivityViewController,共享服务将收到一个字符串,上面写着&#34; Hello world!&#34;。