代码之家  ›  专栏  ›  技术社区  ›  el_quick

如何将自定义函数添加到现有协议中?

  •  0
  • el_quick  · 技术社区  · 5 年前

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

    在我的UICollectionViewCell中 我在想办法增加我的习惯 didCloseItemAt公司 功能如下:

    func collectionView(_ collectionView: UICollectionView, didCloseItemAt indexPath: IndexPath)

    这可能吗?怎样?

    1 回复  |  直到 5 年前
        1
  •  -1
  •   HalR    5 年前

    您可以创建自己的协议,其中包括UICollectionViewDelegate。

    protocol YourCollectionProtocol: UICollectionViewDelegate {
        func collectionView(_ collectionView: UICollectionView, didCloseItemAt indexPath: IndexPath)
    }
    

    然后使用此协议而不是UICollectionViewDelegate。

        2
  •  -1
  •   0-1    5 年前

    extension UICollectionViewDelegate {
        func collectionView(_ collectionView: UICollectionView, didCloseItemAt indexPath: IndexPath) { 
            // Write your code in here
        }
    }
    

    这是如此强大,现在您可以在任何符合UICollectionViewDelegate的对象中调用此方法,并且它将始终运行代码。你只需要写一次!

    推荐文章