Delegates in table view

本文介绍了iOS开发中常见的委托模式概念,通过TableView的具体实例说明了如何将填充单元格的任务委派给数据源对象,并且处理用户点击事件的方式。这种方式使得各组件职责分明,保持系统的简洁性和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The delegation pattern

The concept of delegation is very common in iOS. An object will often rely on another object to help it out with certain tasks. This separation of concerns keeps the system simple as each object does only what it is good at and lets other objects take care of the rest. The table view offers a great examples of this.

Because every app has its own requirements for what its data looks like, the table view must be able to deal with lots of different types of data. Instead of making the table view very complex, or requiring that you modify it to suit your own apps, its designers have chosen to delegate the duty of filling up the cells to another object, the data source.

The table view doesn't really care who its data source is or what kind of data your app deals with, just that it can send the cellForRowAtIndexPath message and that it will receive a cell in return. This keeps the table view component simple and moves the responsibility for handling the data to where it belongs: in your code.

Likewise,the table view knows how to recognise when the user taps a row, but what it should do in response completely depends on the app. In our app we'll make this toggle the checkmark but another app will likely do something totally different. Using the delegation system, the table view can simply send a message that a tap occurred and let the delegate sort it out.


Usually components will have just one delegate but the table view split up its delegate duties into two separate helpers: the UITableViewDataSource for putting rows into the table, and the UITabelDelegate for handling taps on the rows and several other tasks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值