您当前的位置: 首页 >  ios

white camel

暂无认证

  • 1浏览

    0关注

    442博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

iOS 一一 自定义cell按钮的点击事件(通知机制)

white camel 发布时间:2017-08-19 11:28:37 ,浏览量:1

使用通知机制来实现 自定义cell按钮的点击事件.使用通知机制来实现,没有使用代理的方式规范.

代理方式的链接:  http://blog.csdn.net/m0_37989980/article/details/77407073

1. 当点击cell上面的按钮. 发布通知

2. 在控制器的viewDidLoad方法中监听通知.实现监听通知的方法

3. 移除通知

代码如下:

ZYOperationButton文件

#import 

// 自定义按钮

@interface ZYOperationButton : UIButton

@end

@implementation ZYOperationButton

// 当从storyboard中或xib中加载完后会调用这个方法.\
    awakeFromNib是在viewDidLoad之前发生的。就是如果想要对view本身进行一些例如背景颜色,透明度之类的设置就只能在awakeFromNib里面进行,因为view被load之后再改就来不及了,所以只能在view被load之前进行设置,就是awakeFromNib。

- (void)awakeFromNib
{
    [super awakeFromNib];
    // 设置边框的宽度
    self.layer.borderWidth = 1.0f;
    // 设置边框颜色
    self.layer.borderColor = [UIColor orangeColor].CGColor;
    // 设置圆角半径
    self.layer.cornerRadius = self.frame.size.width * 0.5;
    
}

@end
ZYWine文件

#import 

@interface ZYWine : NSObject

/** 图片名 */
@property (nonatomic, copy) NSString *image;

/** 酒的名字 */
@property (nonatomic, copy) NSString *name;

/** 价格 */
@property (nonatomic, copy) NSString *money;

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

@end


@implementation ZYWine

@end
ZYWineCell文件

#import 
@class ZYWine;
@interface ZYWineCell : UITableViewCell

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

@end


#import "ZYWine.h"
@interface ZYWineCell ()
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *title_Label;
@property (weak, nonatomic) IBOutlet UILabel *price_Label;
/** 购买数 */
@property (weak, nonatomic) IBOutlet UILabel *buyCount;

@property (weak, nonatomic) IBOutlet UIButton *minusBtn;

@end

@implementation ZYWineCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // 当加载完storyboard的时候,把减号按钮设为不可点击
    self.minusBtn.enabled = NO;
}

- (void)setWine:(ZYWine *)wine
{
    // 通过酒数据模型设置数据
    _wine = wine;
    self.icon.image = [UIImage imageNamed:wine.image];
    self.title_Label.text = wine.name;
    self.price_Label.text = [NSString stringWithFormat:@"¥%@",wine.money];
    
    // 根据count决定buyCount显示的文字
    self.buyCount.text = [NSString stringWithFormat:@"%d",wine.count];
    
    // 根据count决定减号按钮是否能点击
    self.minusBtn.enabled = (wine.count > 0);
}

#pragma -mark 按钮点击事件
// 这样做的好处: 降低了耦合度,cell和控制器的关联不太大,如果在其他地方要使用到cell,监听cell的通知就可以了
- (IBAction)addBtnClick {
    
    //1. 当点击添加按钮,buyCount增加
    self.wine.count ++;
    
    //2. 修改界面(通过新的模型来修改界面)\
    因为在cell里面不能使用self.tableView reloadData 刷新数据\
    刷新数据的本质就是重新调用数据源方法,通过新的模型设置新的数据,既然如此,我们可以直接在内部通过\
    使用新的模型来设置数据
    self.buyCount.text = [NSString stringWithFormat:@"%d",self.wine.count];
    
    //3. 当点击添加按钮的时候,使减号按钮可以点击
    self.minusBtn.enabled = YES;
    
    //4. 发布通知
    // 发布者是当前的ZYWineCell
    NSNotification *note = [NSNotification notificationWithName:@"addButtonNotificationCenter" object:self userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:note];
    
}

- (IBAction)minusBtnClick {
    
    //1.
    self.wine.count --;
    //2.
    self.buyCount.text = [NSString stringWithFormat:@"%d",self.wine.count];
    
    //3. 当buyCount为0,的时候,设置删除按钮为不可点
    if (self.wine.count == 0) {
        self.minusBtn.enabled = NO;
    }
    
    //4. 发布通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"minusButtonNotificationCenter" object:self userInfo:nil];
}

@end
ViewController文件
#import 

@interface ViewController : UIViewController

@end


#import "ZYWineCell.h"
#import "MJExtension.h"
#import "ZYWine.h"

@interface ViewController () 

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

/** 装所有的酒模型的数组 */
@property (nonatomic,strong) NSArray *wineArray;

@property (weak, nonatomic) IBOutlet UILabel *totalPrice;

@property (weak, nonatomic) IBOutlet UIButton *buyButton;

@property (weak, nonatomic) IBOutlet UIButton *clearButton;
@end

@implementation ViewController

// 懒加载
- (NSArray *)wineArray
{
    if (_wineArray == nil) {
        _wineArray = [ZYWine mj_objectArrayWithFilename:@"wine.plist"];
    }
    return _wineArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 监听加号按钮的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addBtnAction:) name:@"addButtonNotificationCenter" object:nil];
    
    // 监听减号按钮的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minusBtnAction:) name:@"minusButtonNotificationCenter" object:nil];
    
    // 设置一开始没有购买的时候.清除购物车,购买按钮为不可点击
    self.clearButton.enabled = NO;
    self.buyButton.enabled = NO;
    
}

#pragma -mark 移除监听
- (void)dealloc
{
    // 移除控制器中的所有监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma -mark 监听通知的方法
- (void)addBtnAction :(NSNotification *)note
{
    //1. 拿到当前cell对象
    ZYWineCell *cell = note.object;
    //2. 计算总价格(view上的价格 + 酒的价格)
    int totalMoney = self.totalPrice.text.intValue + cell.wine.money.intValue;
    //3. 设置总价格(int类型转为NSString类型)
    self.totalPrice.text = [NSString stringWithFormat:@"%d",totalMoney];
    
    //4. 当监听到加号按钮点击的时候,设置buyButton,clearButton为可点击
    self.buyButton.enabled = YES;
    self.clearButton.enabled = YES;
}

- (void)minusBtnAction :(NSNotification *)note
{
    //1. 拿到当前cell对象
    ZYWineCell *cell = note.object;
    //2. 计算总价格
    int totalMoney = self.totalPrice.text.intValue - cell.wine.money.intValue;
    //3. 设置总价格(int类型转为NSString类型)
    self.totalPrice.text = [NSString stringWithFormat:@"%d",totalMoney];
    
    //4. 当监听到减号按钮点击的时候,设置buyButton,clearButton为可点击
    self.buyButton.enabled = totalMoney > 0;
    self.clearButton.enabled = totalMoney > 0;
}

#pragma -mark 按钮的点击事件
- (IBAction)clearBtn {
    //1. 把totalPrice设为0
    self.totalPrice.text = @"0";
    //2. 遍历所有的模型,把模型的count设为0
    for (ZYWine *wine in self.wineArray) {
        wine.count = 0;
    }
    //3. 刷新表格
    [self.tableView reloadData];
    
    //4. 当点击清空购物车按钮后,设置buyButton,clearButton设为不可点击
    self.buyButton.enabled = NO;
    self.clearButton.enabled = NO;
    
}

- (IBAction)buyBtn {
    
    // 当点击购买按钮的时候.打印购买酒的信息
    for (ZYWine *wine in self.wineArray) {
        if (wine.count) { // 哪个数据模型的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";
    ZYWineCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 设置数据
    cell.wine = self.wineArray[indexPath.row];
    
    return cell;
}

@end

关注
打赏
1661428283
查看更多评论
立即登录/注册

微信扫码登录

0.0391s