gpt4 book ai didi

ios - 如何显示 UICollectionViewCell 的 MenuController?

转载 作者:行者123 更新时间:2023-12-01 16:09:14 25 4
gpt4 key购买 nike

我怎样才能重现 iPhone 上的消息的复制粘贴功能,如果您长按一条消息,消息单元格会变成灰色,并且会出现一个带有“复制”的小弹出窗口。如何在我的 UICollectionViewCells 上显示相同的菜单?

最佳答案

事实证明,他的功能已经内置,只需实现三个 collectionView:委托(delegate)方法。我创建了一个协议(protocol) CopyableCell带有一个名为 copyableProperty 的属性,单元格要复制到剪贴板的字符串,我可以复制的单元格必须遵循。从那时起就很简单了:

  func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
if let _ = collectionView.cellForItemAtIndexPath(indexPath) as? CopyableCell {
return true
}
return false
}
func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
if action.description == "copy:" {
return true
}

return false
}

func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
//No more checking is needed here since we only allow for copying
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CopyableCell {
UIPasteboard.generalPasteboard().string = cell.copyableProperty
}
}

关于ios - 如何显示 UICollectionViewCell 的 MenuController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35034836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com