iOS 一一 自定義cell按鈕的點選事件(通知機制)
阿新 • • 發佈:2019-02-15
使用通知機制來實現 自定義cell按鈕的點選事件.使用通知機制來實現,沒有使用代理的方式規範.
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