委託的意義在於實現多態;在於讓對象可以在程序運行時知足外界對其的改變。html
(1)一個對象屬性、動做,若是在編譯時就能肯定,能夠在這個對象的類裏面來實現。ios
(2)一個對象的屬性、動做,若是在運行時才能肯定,則只能經過這個對象的委託來實現。api
換句話說:類,知足編譯時對對象的設置和要求。委託,用於知足運行時對對象的設置和要求。app
舉個例子:UITableView 的一個實例對象 tableView。ui
當咱們在使用tableView時,咱們在編譯的時候就能肯定下來這個表格的分割線顏色(separatorColor
)、分割線風格(separatorStyle
)、背景圖片(backgroundView)等。還有一些,咱們在編譯的時候就能夠執行的方法。如:spa
– cellForRowAtIndexPath:
– indexPathForCell:
– indexPathForRowAtPoint:
– indexPathsForRowsInRect:
– visibleCells
– indexPathsForVisibleRows
固然,那些只有在運行時才能肯定的方面,咱們只能在類的委託中實現(由於在編譯的時候,咱們沒法肯定其狀態是怎樣的)。以下面這些:設計
– tableView:heightForRowAtIndexPath:
– tableView:estimatedHeightForRowAtIndexPath:
– tableView:indentationLevelForRowAtIndexPath:
– tableView:willDisplayCell:forRowAtIndexPath:
以上,就是對委託的所有理解。若是看着還不過癮,咱們能夠經過以下篇幅,窺視一下蘋果公司是如何設計tableview的。rest
蘋果公司設計tableview在編譯時就要肯定的方面包括:==UITableView Class Referencecode
style
property– numberOfRowsInSection:
– numberOfSections
rowHeight
propertyseparatorStyle
propertyseparatorColor
propertybackgroundView
propertyseparatorInset
property– registerNib:forCellReuseIdentifier:
– registerClass:forCellReuseIdentifier:
– dequeueReusableCellWithIdentifier:forIndexPath:
– dequeueReusableCellWithIdentifier:
– registerNib:forHeaderFooterViewReuseIdentifier:
– registerClass:forHeaderFooterViewReuseIdentifier:
– dequeueReusableHeaderFooterViewWithIdentifier:
tableHeaderView
propertytableFooterView
propertysectionHeaderHeight
propertysectionFooterHeight
property– headerViewForSection:
– footerViewForSection:
– cellForRowAtIndexPath:
– indexPathForCell:
– indexPathForRowAtPoint:
– indexPathsForRowsInRect:
– visibleCells
– indexPathsForVisibleRows
estimatedRowHeight
propertyestimatedSectionHeaderHeight
propertyestimatedSectionFooterHeight
property– scrollToRowAtIndexPath:atScrollPosition:animated:
– scrollToNearestSelectedRowAtScrollPosition:animated:
– indexPathForSelectedRow
– indexPathsForSelectedRows
– selectRowAtIndexPath:animated:scrollPosition:
– deselectRowAtIndexPath:animated:
allowsSelection
propertyallowsMultipleSelection
propertyallowsSelectionDuringEditing
propertyallowsMultipleSelectionDuringEditing
property– beginUpdates
– endUpdates
– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:
– moveRowAtIndexPath:toIndexPath:
– insertSections:withRowAnimation:
– deleteSections:withRowAnimation:
– moveSection:toSection:
editing
property– setEditing:animated:
– reloadData
– reloadRowsAtIndexPaths:withRowAnimation:
– reloadSections:withRowAnimation:
– reloadSectionIndexTitles
dataSource
propertydelegate
propertysectionIndexMinimumDisplayRowCount
propertysectionIndexColor
propertysectionIndexBackgroundColor
propertysectionIndexTrackingBackgroundColor
property
蘋果公司設計tableview在運行時才能肯定的方面包括:==UITableViewDelegate Protocol Referenceorm
– tableView:heightForRowAtIndexPath:
– tableView:estimatedHeightForRowAtIndexPath:
– tableView:indentationLevelForRowAtIndexPath:
– tableView:willDisplayCell:forRowAtIndexPath:
– tableView:accessoryButtonTappedForRowWithIndexPath:
– tableView:accessoryTypeForRowWithIndexPath:
Deprecated in iOS 3.0– tableView:willSelectRowAtIndexPath:
– tableView:didSelectRowAtIndexPath:
– tableView:willDeselectRowAtIndexPath:
– tableView:didDeselectRowAtIndexPath:
– tableView:viewForHeaderInSection:
– tableView:viewForFooterInSection:
– tableView:heightForHeaderInSection:
– tableView:estimatedHeightForHeaderInSection:
– tableView:heightForFooterInSection:
– tableView:estimatedHeightForFooterInSection:
– tableView:willDisplayHeaderView:forSection:
– tableView:willDisplayFooterView:forSection:
– tableView:willBeginEditingRowAtIndexPath:
– tableView:didEndEditingRowAtIndexPath:
– tableView:editingStyleForRowAtIndexPath:
– tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:
– tableView:shouldIndentWhileEditingRowAtIndexPath:
– tableView:didEndDisplayingCell:forRowAtIndexPath:
– tableView:didEndDisplayingHeaderView:forSection:
– tableView:didEndDisplayingFooterView:forSection:
– tableView:shouldShowMenuForRowAtIndexPath:
– tableView:canPerformAction:forRowAtIndexPath:withSender:
– tableView:performAction:forRowAtIndexPath:withSender: