iOS delegation

Think of a UIViewController that you’ve subclassed (call it ViewController) to manage a UICollectionView. ViewController includes a UICollectionView instance (call it collectionView). You set the collectionView instance’s delegate property to self and self is ViewController.

ViewController adopts the UICollectionViewDelegate protocol. The delegate is ViewController. To conform to the UICollectionViewDelegate protocol, ViewControllerimplements methods like collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath).

By implementing the didSelectItemAt method, ViewController gets informed of taps on UICollectionViewCell objects. When a cell is tapped, the ViewController implementation of didSelectItemAt is called. You get to define the body of the didSelectItemAt method so that, for example, when a cell is tapped, you can visually highlight the cell and perform some application-specific logic. collectionView has delegated the responsibility of handling taps on UICollectionViewCell objects to ViewController.

通过实现 didSelectItemAt 方法,当用户点击 UICollectionViewCell 对象时,ViewController 会得到通知。当一个cell被点击,ViewController 的 didSelectItemAt 方法里的实现会被调用。你需要实现didSelectItemAt方法里的内容,这样一来,例如,当一个cell被点击,你就能使这个cell高光显示并且执行一些程序逻辑。collectionView 就把处理UICollectionViewCell 对象上点击事件的责任代理给了ViewController。





转自:Understanding Delegates and Delegation in Swift 4

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 人倒下去的时候,只能是无限的无奈。 一个退休不久的同事阿姨。平时也大大咧咧热热闹闹的一个人。突然就那方面转移了。哎...
    润妈讲堂阅读 1,449评论 0 1
  • 年底了,面临着年终绩效考评、年终调薪、年终评优等等都是对个人一年付出的兑付,回报。 绩效考评鼓励优秀,激励再奋进;...
    绵玥阅读 910评论 0 0
  • 监护室的墙 不薄不厚隔着我你 咱们的距离 不远不近就在那里 你手脚被缚 口不能言 我席地而卧 腰疼(脱)难眠 即使...
    忙里偷闲追梦依然阅读 3,945评论 13 20