代理的简单使用

  • 模型数据

// XMGWine.h
#import 

@interface XMGWine : NSObject
@property (copy, nonatomic) NSString *money;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *image;

/** 购买的数量*/
@property (nonatomic ,assign) int count;
@end

// XMGWine.m
#import "XMGWine.h"

@implementation XMGWine

@end


// XMGWineCell.h
#import 

@class XMGWine,XMGWineCell;

@protocol XMGWineCellDelegate 

@optional

/** 点击了加号按钮 */
- (void)wineCellDidClickPlusButton:(XMGWineCell *)wineCell;
/** 点击了减号按钮 */
- (void)wineCellDidClickMinusButton:(XMGWineCell *)wineCell;

@end

@interface XMGWineCell : UITableViewCell

/** 酒模型*/
@property (nonatomic ,strong) XMGWine *wine;

@property (nonatomic ,weak)id delegate;
@end


// XMGWineCell.m


#import "XMGWineCell.h"
#import "XMGWine.h"
#import "XMGCircleButton.h"

@interface XMGWineCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet XMGCircleButton *minusButton;

@end
@implementation XMGWineCell

- (void)setWine:(XMGWine *)wine
{
    _wine = wine;
    self.imageImageView.image = [UIImage imageNamed:wine.image];

    self.nameLabel.text = wine.name;

    self.moneyLabel.text = wine.money;

    // 根据count决定countLabel显示的文字
    self.countLabel.text = [NSString stringWithFormat:@"%d",wine.count];
    // 根据count决定减号是否能点击
    self.minusButton.enabled = (wine.count > 0);
}

/**
 * 加号点击
 */
- (IBAction)plusClick {
    // 修改模型
    self.wine.count ++ ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];
    // 减号按钮一定能点击
    self.minusButton.enabled = YES;

    // 通知代理(调用代理的方法)
    if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
         [self.delegate wineCellDidClickPlusButton:self];
    }

}

/**
 *  减号点击
 */
- (IBAction)minusClick {
    // 修改模型
    self.wine.count -- ;
    // 修改界面
    self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];

    // 减号按钮不能点击
    if (self.wine.count == 0) {
        self.minusButton.enabled = NO;
    }

    // 通知代理(调用代理的方法)
    if ([self.delegate respondsToSelector:@selector(wineCellDidClickMinusButton:)]) {

        [self.delegate wineCellDidClickMinusButton:self];
    }

}
@end



// ViewController.h
#import 

@interface ViewController : UIViewController


@end

// ViewController.m


#import "ViewController.h"
#import "XMGWine.h"
#import "MJExtension.h"
#import "XMGWineCell.h"

@interface ViewController () 


@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 所有的酒数据*/
@property (nonatomic ,strong) NSArray *wineArray;

/** 总价*/
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@property (weak, nonatomic) IBOutlet UIButton *buyButton;

@end

@implementation ViewController


#pragma mark - 懒加载
- (NSArray *)wineArray
{
    if (!_wineArray) {
        _wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark- XMGWineCellDelegate
// 实现代理方法
- (void)wineCellDidClickPlusButton:(XMGWineCell *)wineCell
{
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue + wineCell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
    // 购买按钮一点能点击
    self.buyButton.enabled = YES;
}

- (void)wineCellDidClickMinusButton:(XMGWineCell *)wineCell
{
    // 计算总价
    int totalPrice = self.totalPriceLabel.text.intValue - wineCell.wine.money.intValue;
    // 设置总价
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
    // 控制减号按钮是否能点击
    self.buyButton.enabled = totalPrice > 0;
}

#pragma mark - 按钮的点击事件
- (IBAction)clear {
    // 修改模型
    for (XMGWine *wine in self.wineArray) {
        wine.count = 0;
    }
    
    // 刷新表格
    [self.tableView reloadData];
    
    // 清空总价
    self.totalPriceLabel.text = @"0";
    
    // 购买按钮一定不能点击
    self.buyButton.enabled = NO;
}

- (IBAction)buy {
    // 打印用户购买的商品的
    for (XMGWine *wine in self.wineArray) {
        if (wine.count > 0) {
            NSLog(@"购买了%d件%@",wine.count,wine.name);
        }
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.wineArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 访问缓存池
    static NSString *ID = @"wine";
    XMGWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.delegate = self;
    // 设置数据
    cell.wine = self.wineArray[indexPath.row];
    return cell;
}
@end


你可能感兴趣的:(代理的简单使用)