协议和委托

简介:

来源:http://blog.csdn.net/duxinfeng2010/article/details/8120960

在iPhone开发协议和委托是常接触到的东西,到底什么是协议什么是委托,他们什么关系?

一 协议

(1)协议相当于没有与类相关联的接口,他申明一组方法,列出他的参数和返回值,共享给其他类使用,然后不进行实现,让用它的类来实现这些方法

(2)在任何一个类中,只有声明了协议,都可以实现协议里的方法。

(3)协议不是一个类,更没有父类了。

(3)协议里面的方法经常都是一些委托方法,

二 委托

委托,故名思议就是托别人办事。打个比方:

张三迫切需要一分工作,但是不知道去哪找。于是他就拜托(委托)李四给帮找一份合适工 作,但是托人办事得给被人好处啊,于是张三给李四塞了一个红包(协议),于是李四通过自己关系在某公司找了一份文秘的工作(实现协议里面委托方法),于然 后他把文秘这份工作给了张三,张三就找到工作了;


三 我们来看一个比较常用的表格单元实现委托和协议

UITableViewDataSource协议和他的委托方法

[cpp]  view plain copy
  1. @protocol UITableViewDataSource<NSObject>  
  2.   
  3. @required  
  4.   
  5. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;  
  6.   
  7. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:  
  8. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)  
  9.   
  10. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;  
  11.   
  12. @optional  
  13.   
  14. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented  
  15.   
  16. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different  
  17. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;  
  18.   
  19. // Editing  
  20.   
  21. // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.  
  22. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;  
  23.   
  24. // Moving/reordering  
  25.   
  26. // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:  
  27. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;  
  28.   
  29. // Index  
  30.   
  31. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    // return list of section titles to display in section index view (e.g. "ABCD...Z#")  
  32. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))  
  33.   
  34. // Data manipulation - insert and delete support  
  35.   
  36. // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change  
  37. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;  
  38.   
  39. // Data manipulation - reorder / moving support  
  40.   
  41. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;  
  42.   
  43. @end  


这是一个完整协议定义

@protocol  协议名

声明方法

@end


但是我们还看到两个特殊关键字 @required  和 @optional

@required 表示我们用到这个协议的时候必须实现这个协议的方法

@optional 表示我们可选择性实现这些方法,看那个需要我们就去实现,不需要的就不实现


UITableViewDelegate协议和委托方法

[cpp]  view plain copy
  1. @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>  
  2.   
  3. @optional  
  4.   
  5. // Display customization  
  6.   
  7. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;  
  8.   
  9. // Variable height support  
  10.   
  11. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;  
  12. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;  
  13. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;  
  14.   
  15. // Section header & footer information. Views are preferred over title should you decide to provide both  
  16.   
  17. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height  
  18. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height  
  19.   
  20. // Accessories (disclosures).   
  21.   
  22. - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0);  
  23. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;  
  24.   
  25. // Selection  
  26.   
  27. // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.  
  28. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;  
  29. - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  30. // Called after the user changes the selection.  
  31. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;  
  32. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  33.   
  34. // Editing  
  35.   
  36. // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.  
  37. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;  
  38. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  39.   
  40. // Controls whether the background is indented while editing.  If not implemented, the default is YES.  This is unrelated to the indentation level below.  This method only applies to grouped style table views.  
  41. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;  
  42.   
  43. // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row  
  44. - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;  
  45. - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;  
  46.   
  47. // Moving/reordering  
  48.   
  49. // Allows customization of the target row for a particular row as it is being moved/reordered  
  50. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;                 
  51.   
  52. // Indentation  
  53.   
  54. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies  
  55.   
  56. // Copy/Paste.  All three methods must be implemented by the delegate.  
  57.   
  58. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);  
  59. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);  
  60. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);  
  61.   
  62. @end  


在用的时候,我们现在声明协议

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface BIDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>  
  4.   
  5. @property (strong, nonatomic) NSDictionary *names;  
  6. @property (strong, nonatomic) NSArray *keys;  
  7. @end  

 

实现UITableViewDataSource  UITableViewDelegate协议里面的委托方法

[cpp]  view plain copy
  1. #pragma mark -  
  2. #pragma mark Table View Data Source Methods  
  3. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  4.     return [keys count];  
  5. }  
  6.   
  7. - (NSInteger)tableView:(UITableView *)tableView  
  8.  numberOfRowsInSection:(NSInteger)section {  
  9.     NSString *key = [keys objectAtIndex:section];  
  10.     NSArray *nameSection = [names objectForKey:key];  
  11.     return [nameSection count];  
  12. }  
  13.   
  14. - (UITableViewCell *)tableView:(UITableView *)tableView  
  15.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  16.     NSUInteger section = [indexPath section];  
  17.     NSUInteger row = [indexPath row];  
  18.       
  19.     NSString *key = [keys objectAtIndex:section];  
  20.     NSArray *nameSection = [names objectForKey:key];  
  21.       
  22.     static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";  
  23.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  24.                              SectionsTableIdentifier];  
  25.     if (cell == nil) {  
  26.         cell = [[UITableViewCell alloc]  
  27.                  initWithStyle:UITableViewCellStyleDefault  
  28.                  reuseIdentifier:SectionsTableIdentifier];  
  29.     }  
  30.       
  31.     cell.textLabel.text = [nameSection objectAtIndex:row];  
  32.     return cell;  
  33. }  
  34.   
  35. - (NSString *)tableView:(UITableView *)tableView  
  36. titleForHeaderInSection:(NSInteger)section {  
  37.     NSString *key = [keys objectAtIndex:section];  
  38.     return key;  
  39. }  
  40.   
  41. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {  
  42.     return keys;  
  43. }  

 

这就就是实现一些里面的委托方法过程运行改程序运行结果

  



该程序源码http://download.csdn.net/detail/duxinfeng2010/4695666 



本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3471071.html,如需转载请自行联系原作者

目录
相关文章
|
C#
多播委托
多播委托
76 0
|
网络架构 Windows
WCF 设计和实现服务协定(01)
作者:jiankunking 出处:http://blog.csdn.net/jiankunking WCF 术语: • 消息 – 消息是一个独立的数据单元,它可能由几个部分组成,包括消息正文和消息头。• 服务 – 服务是一个构造,它公开一个或多个终结点,其中每个终结点都公开一个或多个服务操作。• 终结点 – 终结点是用来发送或接收消息(或执行这两种操作)的构造。 终结点包括一个定义消息可以
1227 0
|
数据安全/隐私保护
委托(二):使用事件来对委托变量进行封装
          接上篇:委托(一):委托与方法                         在以前学习设计模式的时候,我们经常看到这样的代码:                                     可以这样说,委托和事件经常一起使用,感觉挺麻烦的,而且觉得委托已经挺好了,为什么还要加入事件呢?搞得挺复杂的。
943 0