【问题标题】:How to show MenuController of UICollectionViewCell?如何显示 UICollectionViewCell 的 MenuController?
【发布时间】:2016-01-27 10:31:21
【问题描述】:

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

【问题讨论】:

标签: ios swift uicollectionview uimenucontroller uipasteboard


【解决方案1】:

事实证明,他的功能已经内置并且就像实现三个collectionView: 委托方法一样简单。我创建了一个协议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
    }
  }

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多