// XMGWine.h
#import <Foundation/Foundation.h>
@interface XMGWine : NSObject
@property (copy, nonatomic) NSString *money;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *image;
/** 購買的數(shù)量*/
@property (nonatomic ,assign) int count;
@end
// XMGWine.m
#import "XMGWine.h"
@implementation XMGWine
// XMGWineCell.h
#import <UIKit/UIKit.h>
@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;
// 根據(jù)count決定countLabel顯示的文字
self.countLabel.text = [NSString stringWithFormat:@"%d",wine.count];
// 根據(jù)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;
// 發(fā)布通知
/**
* postNotificationName : 通知的名稱
* object : 通知的發(fā)布者
*/
[[NSNotificationCenter defaultCenter] postNotificationName:@"plusButtonClickNotification" object:self];
}
/**
* 減號點擊
*/
- (IBAction)minusClick {
// 修改模型
self.wine.count -- ;
// 修改界面
self.countLabel.text = [NSString stringWithFormat:@"%d",self.wine.count];
// 減號按鈕不能點擊
if (self.wine.count == 0) {
self.minusButton.enabled = NO;
}
// 發(fā)布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"minusButtonClickNotification" object:self];
}
@end
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import "XMGWine.h"
#import "MJExtension.h"
#import "XMGWineCell.h"
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 所有的酒數(shù)據(jù)*/
@property (nonatomic ,strong) NSArray *wineArray;
/** 總價*/
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@end
@implementation ViewController
#pragma mark - 懶加載
- (NSArray *)wineArray
{
if (!_wineArray) {
_wineArray = [XMGWine mj_objectArrayWithFilename:@"wine.plist"];
}
return _wineArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 監(jiān)聽通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
/**
* @param addObserver: 誰接收這個通知
* @param selector: 調用什么方法
* @param name: 通知的名稱
* @param object: 通知發(fā)布者
*/
[center addObserver:self selector:@selector(plusClick:) name:@"plusButtonClickNotification" object:nil];
[center addObserver:self selector:@selector(minusClick:) name:@"minusButtonClickNotification" object:nil];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 通知的監(jiān)聽方法
- (void)plusClick:(NSNotification *)note
{
// 通知發(fā)布者
XMGWineCell *cell = note.object;
// 計算總價
int totalPrice = self.totalPriceLabel.text.intValue + cell.wine.money.intValue;
// 設置總價
self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}
- (void)minusClick:(NSNotification *)note
{
// 通知發(fā)布者
XMGWineCell *cell = note.object;
// 計算總價
int totalPrice = self.totalPriceLabel.text.intValue - cell.wine.money.intValue;
// 設置總價
self.totalPriceLabel.text = [NSString stringWithFormat:@"%d",totalPrice];
}
#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];
// 設置數(shù)據(jù)
cell.wine = self.wineArray[indexPath.row];
return cell;
}
@end
// 發(fā)出通知
[LZNotificationCenter postNotificationName:LZUpdateLocationNotification object:nil userInfo:@{@"location" : userLocation}];
// 添加監(jiān)聽事件
- (void)setupNote
{
[LZNotificationCenter addObserver:self selector:@selector(updateLocation:) name:LZUpdateLocationNotification object:nil];
}
- (void)updateLocation:(NSNotification *)noti{
// NSLog(@"%@", noti.userInfo[@"location"]);
BMKUserLocation *userLocation = noti.userInfo[@"location"];
NSLog(@"latitude is %f", userLocation.location.coordinate.latitude);
NSLog(@"longitude is %f", userLocation.location.coordinate.longitude);
NSString *str = [NSString stringWithFormat:@"%f,%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude];
NSData *data1 = [str dataUsingEncoding:NSUTF8StringEncoding];
// sendData方法返回值是void,發(fā)送數(shù)據(jù)成功之后,
[self.mConnection sendData:data1 toHost:@"192.168.2.10" port:18601 withTimeout:-1 tag:0];
}
- (void)dealloc
{
// 移除通知
[LZNotificationCenter removeObserver:self];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者