我目前正在实现一个UICollectionView来在我的应用程序中显示CoreData数据,我想使用ContextMenu为每个单元格添加“删除”功能。
在互联网上,我观察到许多页面能够在UITableView和UICollectionView的单元格中自定义UIMenuController,但我仍然无法显示我的MenuItem“删除”。
这是我的代码:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<!-- ********* CHECK HERE ********* -->
<item name="android:actionBarSize">250dp</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
的CollectionView:
private func config_menu()
{
let menuItem = UIMenuItem(title: "Test", action: #selector(test(_:)))
let menu = UIMenuController.shared
menu.menuItems = [menuItem]
menu.update()
}
@objc func test(_ sender: Any?)
{
}
有什么问题?
答案 0 :(得分:1)
我找到了解决方案:D能够使用自定义MenuItems,有必要在单元格的子类中声明它们,这样::
class Cell_Prueba: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(test(_:)))
}
@objc func test(_ sender: Any?)
{
//Do Any
}
}