ios - UIContextMenu 和 UITableView 问题
问题描述
我正在尝试添加一个类似的 UIContextMenu,就像您在 iMessages 中找到的那样。长按消息时,应显示带有一些选项的上下文菜单。我使用tableView(_:contextMenuConfigurationForRowAt:point:)和其他方法。到目前为止,一切都很好。
但是我遇到了两个我无法解决的问题:
最大的一个是预览的变化。当显示上下文菜单并且您收到一条新消息(导致 tableview 重新加载)时,预览将更改其内容。突然之间,出现了与您最初选择的不同的消息。但我不明白为什么,因为没有调用上下文菜单的 tableview 方法......我怎么能解决这个问题?Apple 信息停止添加新信息。但例如 Viber 仍然能够在有上下文菜单时接收新消息。
我想在
tableView(_:willDisplayContextMenu:animator:)
...的帮助下以某种方式像Apple一样处理它,但是还有第二个问题-这种方法仅适用于iOS +14.0 ..!所以我怎么知道在 iOS 14 之前会有上下文菜单?
我会很感激任何帮助。谢谢。
解决方案
我能够以某种方式解决第一个问题。主要思想是使用快照而不是单元格的视图。这样即使 tableView 重新加载,快照也保持不变。
您必须实现这两种方法并在那里提供快照: tableView(_:previewForHighlightingContextMenuWithConfiguration:) tableView(_:previewForDismissingContextMenuWithConfiguration:)
在我的代码中,它看起来像这样:
func tableView(_: UITableView, previewForHighlightingContextMenuWithConfiguration configuration: UIContextMenuConfiguration) -> UITargetedPreview? {
guard
let messageId = configuration.identifier as? String,
let indexPath = dataSource.indexPath(for: messageId),
let cell = tableView.cellForRow(at: indexPath) as? YourCustomCell else {
return nil
}
return makeTargetedPreview(cell: cell)
}
func makeTargetedPreview(cell: YourCustomCell) -> UITargetedPreview? {
guard
let previewView = cell.viewYouWantToDisplay,
let snapshot = previewView.snapshotView(afterScreenUpdates: false)
else {
return nil
}
// 1. Prepare how should the view in the preview look like
let parameters = UIPreviewParameters()
parameters.backgroundColor = .clear
parameters.visiblePath = UIBezierPath(roundedRect: previewView.bounds, cornerRadius: previewView.layer.cornerRadius)
// 2. Prepare UIPreviewTarget so we can use snapshot
let previewTarget = UIPreviewTarget(
container: previewView,
center: CGPoint(x: previewView.bounds.midX, y: previewView.bounds.midY)
)
// 3. Return UITargetedPreview with snapshot
// important ! We can't use cell's previewView directly as it's getting changed when data reload happens
return UITargetedPreview(view: snapshot, parameters: parameters, target: previewTarget)
}
注意:实现previewForDismissing(...)
类似。