委托的意义在于实现多态;在于让对象能够在程序运行时满足外界对其的改变。
(1)一个对象属性、动作,如果在编译时就能确定,可以在这个对象的类里面来实现。
(2)一个对象的属性、动作,如果在运行时才能确定,则只能通过这个对象的委托来实现。
换句话说:类,满足编译时对对象的设置和要求。委托,用于满足运行时对对象的设置和要求。
举个例子:UITableView 的一个实例对象 tableView。
当我们在使用tableView时,我们在编译的时候就能确定下来这个表格的分割线颜色(separatorColor )、分割线风格(separatorStyle )、背景图片(backgroundView)等。还有一些,我们在编译的时候就可以执行的方法。如:
– cellForRowAtIndexPath:– indexPathForCell:– indexPathForRowAtPoint:– indexPathsForRowsInRect:– visibleCells– indexPathsForVisibleRows当然,那些只有在运行时才能确定的方面,我们只能在类的委托中实现(因为在编译的时候,我们无法确定其状态是怎样的)。如下面这些:
– tableView:heightForRowAtIndexPath:– tableView:estimatedHeightForRowAtIndexPath:– tableView:indentationLevelForRowAtIndexPath:– tableView:willDisplayCell:forRowAtIndexPath:以上,就是对委托的全部理解。如果看着还不过瘾,我们可以通过如下篇幅,窥视一下苹果公司是如何设计tableview的。
苹果公司设计tableview在编译时就要确定的方面包括:==UITableView Class Reference
style property– numberOfRowsInSection:– numberOfSectionsrowHeight 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– indexPathsForVisibleRowsestimatedRowHeight 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:– reloadSectionIndexTitlesdataSource propertydelegate propertysectionIndexMinimumDisplayRowCount propertysectionIndexColor propertysectionIndexBackgroundColor propertysectionIndexTrackingBackgroundColor property
苹果公司设计tableview在运行时才能确定的方面包括:==UITableViewDelegate Protocol Reference
– 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:原文:http://www.cnblogs.com/ygm900/p/3598527.html