KVO的简单使用

  • 模型数据

// 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;
@interface XMGWineCell : UITableViewCell

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

@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:@"%zd",wine.count];
    // 根据count决定减号是否能点击
    self.minusButton.enabled = (wine.count > 0);
}

// KVO

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

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

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

}
@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


- (NSArray *)wineArray
{
    if (!_wineArray) {
        _wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
        // KVO监听
        for (XMGWine *wine in _wineArray) {
            // 监听模型的count属性
            [wine addObserver:self forKeyPath:@"count" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//            NSLog(@"%@",[wine valueForKeyPath:@"isa"]);
        }
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)dealloc{
    // 移除监听
    for (XMGWine *wine in _wineArray) {
        [wine removeObserver:self forKeyPath:@"count"];
    }
}

#pragma mark - KVO的监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(XMGWine *)wine change:(NSDictionary *)change context:(void *)context
{
    // NSKeyValueChangeNewKey == @"new"
    int new = [change[NSKeyValueChangeNewKey] intValue];
    // NSKeyValueChangeOldKey == @"old"
    int old = [change[NSKeyValueChangeOldKey] intValue];

    if (new > old) { // 点击加号
        // 计算总价
        int totalPrice = self.totalPriceLabel.text.intValue + wine.money.intValue;
        // 设置总价
        self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
        // 购买按钮一点能点击
        self.buyButton.enabled = YES;
    } else { // 点击减号
        // 计算总价
        int totalPrice = self.totalPriceLabel.text.intValue - 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.wine = self.wineArray[indexPath.row];
    return cell;
}
@end


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