iOS设计模式 - 代理设计模式(Delegate)

摘要 :代理模式 顾名思义就是委托

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>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值