[通俗易懂]理解「委託」

委託的意義在於實現多態;在於讓對象可以在程序運行時知足外界對其的改變。html

(1)一個對象屬性、動做,若是在編譯時就能肯定,能夠在這個對象的類裏面來實現。ios

(2)一個對象的屬性、動做,若是在運行時才能肯定,則只能經過這個對象的委託來實現。api

換句話說:類,知足編譯時對對象的設置和要求。委託,用於知足運行時對對象的設置和要求。app

舉個例子:UITableView 的一個實例對象 tableView。ui

當咱們在使用tableView時,咱們在編譯的時候就能肯定下來這個表格的分割線顏色(separatorColor )、分割線風格(separatorStyle )、背景圖片(backgroundView)等。還有一些,咱們在編譯的時候就能夠執行的方法。如:spa

固然,那些只有在運行時才能肯定的方面,咱們只能在類的委託中實現(由於在編譯的時候,咱們沒法肯定其狀態是怎樣的)。以下面這些:設計

以上,就是對委託的所有理解。若是看着還不過癮,咱們能夠經過以下篇幅,窺視一下蘋果公司是如何設計tableview的。rest

蘋果公司設計tableview在編譯時就要肯定的方面包括:==UITableView Class Referencecode

 

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 Referenceorm

 

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

相關文章
相關標籤/搜索