ios协议和委托
在iPhone开发协议和委托是常接触到的东西,到底什么是协议什么是委托,他们什么关系?
一 协议
(1)协议相当于没有与类相关联的接口,他申明一组方法,列出他的参数和返回值,共享给其他类使用,然后不进行实现,让用它的类来实现这些方法
(2)在任何一个类中,只有声明了协议,都可以实现协议里的方法。
(3)协议不是一个类,更没有父类了。
(3)协议里面的方法经常都是一些委托方法,
二 委托
委托,故名思议就是托别人办事。打个比方:
张三迫切需要一分工作,但是不知道去哪找。于是他就拜托(委托)李四给帮找一份合适工作,但是托人办事得给被人好处啊,于是张三给李四塞了一个红包(协议),于是李四通过自己关系在某公司找了一份文秘的工作(实现协议里面委托方法),于然后他把文秘这份工作给了张三,张三就找到工作了;
三 我们来看一个比较常用的表格单元实现委托和协议
UITableViewDataSource协议和他的委托方法
- @protocol UITableViewDataSource<NSObject>
- @required
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
- // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- @optional
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
- - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
- // Editing
- // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- // Moving/reordering
- // 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:
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- // Index
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#")
- - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1))
- // Data manipulation - insert and delete support
- // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- // Data manipulation - reorder / moving support
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
- @end
这是一个完整协议定义
@protocol 协议名
声明方法
@end
但是我们还看到两个特殊关键字 @required 和 @optional
@required 表示我们用到这个协议的时候必须实现这个协议的方法
@optional 表示我们可选择性实现这些方法,看那个需要我们就去实现,不需要的就不实现
UITableViewDelegate协议和委托方法
- @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
- @optional
- // Display customization
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- // Variable height support
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
- // Section header & footer information. Views are preferred over title should you decide to provide both
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
- // Accessories (disclosures).
- - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0);
- - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
- // Selection
- // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- // Called after the user changes the selection.
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- // Editing
- // 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.
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- // 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.
- - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- // 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
- - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- // Moving/reordering
- // Allows customization of the target row for a particular row as it is being moved/reordered
- - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
- // Indentation
- - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies
- // Copy/Paste. All three methods must be implemented by the delegate.
- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- @end
在用的时候,我们现在声明协议
- #import <UIKit/UIKit.h>
- @interface BIDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
- @property (strong, nonatomic) NSDictionary *names;
- @property (strong, nonatomic) NSArray *keys;
- @end
实现UITableViewDataSource UITableViewDelegate协议里面的委托方法
- #pragma mark -
- #pragma mark Table View Data Source Methods
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [keys count];
- }
- - (NSInteger)tableView:(UITableView *)tableView
- numberOfRowsInSection:(NSInteger)section {
- NSString *key = [keys objectAtIndex:section];
- NSArray *nameSection = [names objectForKey:key];
- return [nameSection count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- NSUInteger section = [indexPath section];
- NSUInteger row = [indexPath row];
- NSString *key = [keys objectAtIndex:section];
- NSArray *nameSection = [names objectForKey:key];
- static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- SectionsTableIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc]
- initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:SectionsTableIdentifier];
- }
- cell.textLabel.text = [nameSection objectAtIndex:row];
- return cell;
- }
- - (NSString *)tableView:(UITableView *)tableView
- titleForHeaderInSection:(NSInteger)section {
- NSString *key = [keys objectAtIndex:section];
- return key;
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- return keys;
- }
这就就是实现一些里面的委托方法过程运行改程序运行结果

该程序源码http://download.csdn.net/detail/duxinfeng2010/4695666
ios协议和委托的更多相关文章
- IOS Objective-C 协议,委托
IOS Objective-C 协议,委托 IOS开发使用的语言Objective-C(以下简称OBJ-C)是一种扩展自C语言的面向对象语言.在OBJ-C中有一个很重要概念:消息.在最近的学习当中逐渐 ...
- 【IOS学习】之四、协议,委托,分类粗解
何为协议,何为委托,何为分类(类别)? 委托 即 代理 delegate: 他是 (接口的实现类)类似c中的回调. 把某个对象要做的事情委托给别的对象去做. 那么别的对象就是这个对 ...
- iOS delegate, 代理/委托与协议.
之前知知道iOS协议怎么写, 以为真的跟特么java接口一样, 后来发现完全不是. 首先, 说说应用场景, 就是当你要用一个程序类, 或者说逻辑类, 去控制一个storyboard里面的label, ...
- 简单例子让你很好的理解:协议与委托 (Protocol and Delegate)
1 协议: 协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法. 它是对对象行为的定义,也是对功能的规范. 示例: 1 2 3 4 5 6 7 8 9 // GoodChild.h ...
- iOS 设计模式-委托模式
委托是指给一个对象提供机会对另一对象中的变化做出反应或者相应另一个对象的行为.其基本思想是协同解决问题. Delegate的使用场合 对象A内部发生了一些事情,想通知对象B 对象B想监听对象A内部发生 ...
- Jquery 在ios上事件委托失效
点击通过js遍历出来的列表,跳转页面.点击事件委托在document上, 像这样: $(document).on("click",".nav",function ...
- IOS开发使用委托delegate在不同窗口之间传递数据
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...
- [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...
- IOS开发之----协议与委托(Protocol and Delegate) 实例解析
1 协议: 协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法. 它是对对象行为的定义,也是对功能的规范. 在写示例之前我给大家说下@required和@optional这两个关键 ...
随机推荐
- Tirp(状压DP)
Description 有一个N*N的迷宫,其中有一些宝藏,现在,小A要从入口(1,1)出发,到达出口(N,N),每次,小A只能从当前的格子走到上下左右四个格子,为了不空手而归,小A决定要拿到所以的宝 ...
- C 其他一些
1.联合,语法 union{成员1;成员2;} 联合间的成员共享同一个空间,也就是说,成员1赋值如果没有被清除,会体现在成员2上 2.位字段,结合结构体瓜分完整的类型存储空间,如 struct{ ; ...
- Loadrunner Http接口Get/Post方法性能测试脚本解析
最近使用LoadRunner 11进行了一次完整的Http WEB接口性能测试,下面介绍下Http接口Get/Post方法性能测试脚本通用编写方法. 1. Http接口性能测试基本流程 首先定义了一个 ...
- idea中的汉语注释出现乱码的解决方案
日记 - idea中的汉语注释出现乱码的解决方案 我是个idea的忠实用户,新公司的项目都是用eclipse做的,通过svn拉下代码后发现,注释的内容里,中文内容都是乱码.问过项目负责人,说可能是GB ...
- javascript 之作用域链-07
复习作用域 上一节我们说到作用域:是指变量可以访问的范围,他规定了如何查找变量,以及确定当前执行代码对变量的访问权限:也说到静态作用域即词法作用域,是在编译阶段决定变量的引用(由程序定义的位置决定,和 ...
- [Bayesian] “我是bayesian我怕谁”系列 - Variational Inference
涉及的领域可能有些生僻,骗不了大家点赞.但毕竟是人工智能的主流技术,在园子却成了非主流. 不可否认的是:乃值钱的技术,提高身价的技术,改变世界观的技术. 关于变分,通常的课本思路是: GMM --&g ...
- python、java和php的百度指数对比
Python: Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议.Python语法简洁清晰,特色之一是强制用空白符( ...
- CREATE DATABASE RoomReservation
要从我的模型开始构建我的RoomReservation数据库对象,我将创建表对象. 要在SQL Server中创建表,我需要使用CREATE TABLE语句. 使用CREATE TABLE语句,我将能 ...
- EasyUI实现购物车、菜单和窗口栏等最常用的用户界面功能
一.EasyUI jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用户界面插件. easyui 提供建立现代化的具有交互性的 javascript 应用的必要的功能. 使用 e ...
- JSP异常之org.apache.jasper.JasperException(转)
According to TLD or attribute directive in tag file, attribute items does not accep t any expression ...