iOS 一一 自定义cell按钮的点击事件(通知机制)
来源:互联网 发布:机器人单片机 编辑:程序博客网 时间:2024/05/16 12:25
使用通知机制来实现 自定义cell按钮的点击事件.使用通知机制来实现,没有使用代理的方式规范.
代理方式的链接: http://blog.csdn.net/m0_37989980/article/details/77407073
1. 当点击cell上面的按钮. 发布通知
2. 在控制器的viewDidLoad方法中监听通知.实现监听通知的方法
3. 移除通知
代码如下:
ZYOperationButton文件
#import <UIKit/UIKit.h>// 自定义按钮@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 <Foundation/Foundation.h>@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 <UIKit/UIKit.h>@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 <UIKit/UIKit.h>@interface ViewController : UIViewController@end#import "ZYWineCell.h"#import "MJExtension.h"#import "ZYWine.h"@interface ViewController () <UITableViewDataSource>@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
阅读全文
0 0
- iOS 一一 自定义cell按钮的点击事件(通知机制)
- iOS 一一 自定义cell按钮的点击事件(代理)
- iOS 一一 自定义cell按钮的点击事件(KVO)
- iOS 自定义Cell按钮的点击代理事件
- 自定义Cell中按钮的点击事件
- iOS 一一 通知机制
- ios tableView 自定义cell上的按钮点击事件不好用
- UITableView自定义cell上的按钮点击事件
- 自定义cell中的按钮点击事件
- cell上按钮的点击事件
- ios tableviewcell上点击按钮禁用时点击走了cell点击事件的解决办法
- iOS点击Cell中的按钮时,取所在的Cell
- iOS 8自定义cell的删除按钮
- IOS应用开发03——自定义Cell的AccessoryButton及事件源被点击的cell行
- 自定义按钮点击事件
- iOS自定义按钮删除Cell
- iOS点击cell上的某一个按钮时 如何获得点击的是第几个cell
- IOS-监听UIAlertView按钮的点击事件
- 欢迎使用CSDN-markdown编辑器
- 面团点评笔试:整数加法
- Python 2 to 3
- 设计模式之静态代理模式
- 读《大道至简》有感
- iOS 一一 自定义cell按钮的点击事件(通知机制)
- 第三天:css选择器的优先级、Adobe photoshop CS6软件、html初步布局
- 采用lombok简化代码
- node.js遇到的错误
- Spring Boot 视频
- spring schema 扩展(NamespaceHandler,BeanDefinitionParser)
- 分布式系统中的必备良药 —— 服务治理
- composer安装 laravel 框架
- Hive简介