摘要 :代理模式 顾名思义就是委托
iOS中经常会遇到的两种情况:
1.使用系统自带的代理方法
2.自定义代理方法
一、代理的定义和使用方法
请代理的三步!!!
1.创建代理
@protocol YZHTgFooterViewDelegate <NSObject>
@optional
- (void) tgFooterViewClickLoadBtn:(YZHTgFooterView *)footerView;
@end
代理名字的创建规则为当前的类名后边加上Delegate
2.设置delegate属性
@property(nonatomic, weak) id<YZHTgFooterViewDelegate> delegate;
3.判断是否实现代理
if ([self.delegate respondsToSelector:@selector(tgFooterViewClickLoadBtn:)]) {
[self.delegate tgFooterViewClickLoadBtn:self];
}
实现代理的三步!!!!
1. 遵守代理
@interface YZHViewController ()<UITableViewDataSource,UITableViewDelegate>
2. 设置delegate
footerView.delegate = self;
3. 实现代理方法
<span style="font-size:14px;"><span style="background-color: rgb(255, 255, 255);"><span style="color:#000000;">- (void)tgFooterViewClickLoadBtn:(YZHTgFooterView *)footerView
{
YZHTg *tg = [[YZHTg alloc] init];
[self.tgs addObject:tg];
[self.tableView reloadData];
}</span></span></span>
二、系统自带的代理方法
系统自带的代理模式,以UITableView为例
1.在UITableView中声明UIScrollViewDelegate 和 UITableViewDataSource
定义代理方法
<span style="font-size:14px;">@optional 表示可以不实现的方法</span>
<span style="font-size:14px;">@required 表示必须实现的方法</span>
<span style="font-size:14px;">
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
@optional
// Display customization
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
..........
@end</span>
<span style="font-size:14px;">
@protocol UITableViewDataSource<NSObject>
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
....
@end</span>
2.在控制器中遵守UITableView的协议
<span style="font-size:14px;">@interface YZHViewController ()<UITableViewDataSource,UITableViewDelegate></span>
设置当前控制器为UITableView的代理和数据源为self
<span style="font-size:14px;"> self.tableView.dataSource = self;
self.tableView.delegate = self;</span>
实现代理和数据源方法
<span style="font-size:14px;">#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YZHTgCell *cell = [YZHTgCell tgCellWithTableView:tableView];
cell.tg = self.tgs[indexPath.row];
return cell;
}
#pragma mark - 代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}</span>