委托的意义在于实现多态;在于让对象可以在程序运行时知足外界对其的改变。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: