[通俗易懂]理解“委托”

委托的意义在于实现多态;在于让对象能够在程序运行时满足外界对其的改变。

(1)一个对象属性、动作,如果在编译时就能确定,可以在这个对象的类里面来实现。

(2)一个对象的属性、动作,如果在运行时才能确定,则只能通过这个对象的委托来实现。

换句话说:类,满足编译时对对象的设置和要求。委托,用于满足运行时对对象的设置和要求。

举个例子:UITableView 的一个实例对象 tableView。

当我们在使用tableView时,我们在编译的时候就能确定下来这个表格的分割线颜色(separatorColor )、分割线风格(separatorStyle )、背景图片(backgroundView)等。还有一些,我们在编译的时候就可以执行的方法。如:

当然,那些只有在运行时才能确定的方面,我们只能在类的委托中实现(因为在编译的时候,我们无法确定其状态是怎样的)。如下面这些:

以上,就是对委托的全部理解。如果看着还不过瘾,我们可以通过如下篇幅,窥视一下苹果公司是如何设计tableview的。

苹果公司设计tableview在编译时就要确定的方面包括:==UITableView Class Reference

Tasks

Initializing a UITableView Object

Configuring a Table View

Creating Table View Cells

Accessing Header and Footer Views

Accessing Cells and Sections

Estimating Element Heights

Scrolling the Table View

Managing Selections

Inserting, Deleting, and Moving Rows and Sections

Managing the Editing of Table Cells

Reloading the Table View

Accessing Drawing Areas of the Table View

Managing the Delegate and the Data Source

Configuring the Table Index

苹果公司设计tableview在运行时才能确定的方面包括:==UITableViewDelegate Protocol Reference

Tasks

Configuring Rows for the Table View

Managing Accessory Views

Managing Selections

Modifying the Header and Footer of Sections

Editing Table Rows

Reordering Table Rows

Tracking the Removal of Views

Copying and Pasting Row Content

Managing Table View Highlighting

原文地址:https://www.cnblogs.com/ygm900/p/3598527.html