[通俗易懂]理解“委托”
委托的意义在于实现多态;在于让对象能够在程序运行时满足外界对其的改变。
(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
Tasks
Initializing a UITableView Object
Configuring a Table View
-
styleproperty – numberOfRowsInSection:– numberOfSections-
rowHeightproperty -
separatorStyleproperty -
separatorColorproperty -
backgroundViewproperty -
separatorInsetproperty
Creating Table View Cells
– registerNib:forCellReuseIdentifier:– registerClass:forCellReuseIdentifier:– dequeueReusableCellWithIdentifier:forIndexPath:– dequeueReusableCellWithIdentifier:
Accessing Header and Footer Views
– registerNib:forHeaderFooterViewReuseIdentifier:– registerClass:forHeaderFooterViewReuseIdentifier:– dequeueReusableHeaderFooterViewWithIdentifier:-
tableHeaderViewproperty -
tableFooterViewproperty -
sectionHeaderHeightproperty -
sectionFooterHeightproperty – headerViewForSection:– footerViewForSection:
Accessing Cells and Sections
– cellForRowAtIndexPath:– indexPathForCell:– indexPathForRowAtPoint:– indexPathsForRowsInRect:– visibleCells– indexPathsForVisibleRows
Estimating Element Heights
-
estimatedRowHeightproperty -
estimatedSectionHeaderHeightproperty -
estimatedSectionFooterHeightproperty
Scrolling the Table View
– scrollToRowAtIndexPath:atScrollPosition:animated:– scrollToNearestSelectedRowAtScrollPosition:animated:
Managing Selections
– indexPathForSelectedRow– indexPathsForSelectedRows– selectRowAtIndexPath:animated:scrollPosition:– deselectRowAtIndexPath:animated:-
allowsSelectionproperty -
allowsMultipleSelectionproperty -
allowsSelectionDuringEditingproperty -
allowsMultipleSelectionDuringEditingproperty
Inserting, Deleting, and Moving Rows and Sections
– beginUpdates– endUpdates– insertRowsAtIndexPaths:withRowAnimation:– deleteRowsAtIndexPaths:withRowAnimation:– moveRowAtIndexPath:toIndexPath:– insertSections:withRowAnimation:– deleteSections:withRowAnimation:– moveSection:toSection:
Managing the Editing of Table Cells
-
editingproperty – setEditing:animated:
Reloading the Table View
– reloadData– reloadRowsAtIndexPaths:withRowAnimation:– reloadSections:withRowAnimation:– reloadSectionIndexTitles
Accessing Drawing Areas of the Table View
Managing the Delegate and the Data Source
-
dataSourceproperty -
delegateproperty
Configuring the Table Index
-
sectionIndexMinimumDisplayRowCountproperty -
sectionIndexColorproperty -
sectionIndexBackgroundColorproperty -
sectionIndexTrackingBackgroundColorproperty
苹果公司设计tableview在运行时才能确定的方面包括:==UITableViewDelegate Protocol Reference
Tasks
Configuring Rows for the Table View
– tableView:heightForRowAtIndexPath:– tableView:estimatedHeightForRowAtIndexPath:– tableView:indentationLevelForRowAtIndexPath:– tableView:willDisplayCell:forRowAtIndexPath:
Managing Accessory Views
– tableView:accessoryButtonTappedForRowWithIndexPath:– tableView:accessoryTypeForRowWithIndexPath:Deprecated in iOS 3.0
Managing Selections
– tableView:willSelectRowAtIndexPath:– tableView:didSelectRowAtIndexPath:– tableView:willDeselectRowAtIndexPath:– tableView:didDeselectRowAtIndexPath:
Modifying the Header and Footer of Sections
– tableView:viewForHeaderInSection:– tableView:viewForFooterInSection:– tableView:heightForHeaderInSection:– tableView:estimatedHeightForHeaderInSection:– tableView:heightForFooterInSection:– tableView:estimatedHeightForFooterInSection:– tableView:willDisplayHeaderView:forSection:– tableView:willDisplayFooterView:forSection:
Editing Table Rows
– tableView:willBeginEditingRowAtIndexPath:– tableView:didEndEditingRowAtIndexPath:– tableView:editingStyleForRowAtIndexPath:– tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:– tableView:shouldIndentWhileEditingRowAtIndexPath:
Reordering Table Rows
Tracking the Removal of Views
– tableView:didEndDisplayingCell:forRowAtIndexPath:– tableView:didEndDisplayingHeaderView:forSection:– tableView:didEndDisplayingFooterView:forSection:
Copying and Pasting Row Content
– tableView:shouldShowMenuForRowAtIndexPath:– tableView:canPerformAction:forRowAtIndexPath:withSender:– tableView:performAction:forRowAtIndexPath:withSender:
浙公网安备 33010602011771号