123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // PdProductDetailTableViewCell.m
- // FirstLink
- //
- // Created by 王孝道 on 15/6/1.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "ProductDetailTableCell.h"
- @interface ProductDetailTableCell ()
- @property (nonatomic, strong) UIButton *addBtn;
- @property (nonatomic, strong) UIButton *reduceBtn;
- @property (nonatomic, strong) UILabel *numberLabel;
- @property (nonatomic, strong) UILabel *groupNumberLabel; // 团购时用来显示数量
- @end
- @implementation ProductDetailTableCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self initialize];
- self.selectionStyle = UITableViewCellSelectionStyleNone;
- }
- return self;
- }
- - (void)initialize
- {
- UIView *bottomLine = [[UIView alloc]init];
- bottomLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
-
- [self.contentView addSubview:self.productImgView];
- [self.contentView addSubview:self.descripLabel];
- [self.contentView addSubview:self.priceLabel];
- [self.contentView addSubview:self.specLabel];
- [self.contentView addSubview:self.addBtn];
- [self.contentView addSubview:self.reduceBtn];
- [self.contentView addSubview:self.numberLabel];
- [self.contentView addSubview:bottomLine];
-
- UIView *superView = self.contentView;
-
- [self.productImgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(superView).offset(15);
- make.centerY.equalTo(superView);
- make.width.height.equalTo(@70);
- }];
-
- [self.descripLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.productImgView.mas_right).offset(15);
- make.right.lessThanOrEqualTo(self.priceLabel.mas_left).offset(- 15);
- make.top.equalTo(self.productImgView);
- }];
-
- [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(superView).offset(- 15);
- make.top.equalTo(self.descripLabel);
- }];
- [self.specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.descripLabel);
- make.top.equalTo(self.descripLabel.mas_bottom);
- make.right.equalTo(self.reduceBtn.mas_left).offset(- 5);
- make.bottom.equalTo(self.productImgView).offset(- 5);
- }];
-
- [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(superView).offset(- 15);
- make.bottom.equalTo(self.productImgView);
- }];
-
- [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.addBtn.mas_left).offset(- 3);
- make.centerY.equalTo(self.addBtn);
- make.width.equalTo(@60);
- make.height.equalTo(self.addBtn);
- }];
-
- [self.contentView addSubview:self.groupNumberLabel];
- [self.groupNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.priceLabel.mas_right);
- make.top.equalTo(self.priceLabel.mas_bottom);
- make.width.mas_equalTo(60);
- make.height.mas_equalTo(20);
- }];
-
- [self.reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.numberLabel.mas_left).offset(- 3);
- make.centerY.equalTo(self.addBtn);
- }];
-
- [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.bottom.equalTo(self.contentView);
- make.height.equalTo(@0.5);
- make.left.equalTo(self.contentView).offset(15);
- }];
- }
- + (NSString *)cdnImgUrlStringWithString:(NSString *)imgUrl
- {
- return imgUrl;
- // return [NSString stringWithFormat:@"%@%@", imgUrl,
- // [FLStringHelper cdnParamaterString:70 height:70]];
- }
- - (void)configCellForNormal {
- self.addBtn.hidden = NO;
- self.reduceBtn.hidden = NO;
- self.numberLabel.hidden = NO;
- self.groupNumberLabel.hidden = YES;
- }
- - (void)configCellForGroup {
- self.addBtn.hidden = YES;
- self.reduceBtn.hidden = YES;
- self.numberLabel.hidden = YES;
- self.groupNumberLabel.hidden = NO;
- }
- #pragma mark - action
- - (void)numberChangeBtnClick:(UIButton *)sender{
- NSUInteger number = self.numberLabel.text.integerValue;
- if (sender.tag == 0) {
- number ++;
- if (self.numberChangeAction) {
- if (!self.numberChangeAction(number, self.indexPath)) return;
- }
- self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
- }else if (sender.tag == 1){
- number --;
- if (number > 0) self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
- if (self.numberChangeAction) self.numberChangeAction(number, self.indexPath);
- }
- }
- #pragma mark - getter && setter
- - (UIImageView *)productImgView
- {
- if (_productImgView == nil) {
- _productImgView = [[UIImageView alloc]init];
- _productImgView.contentMode = UIViewContentModeScaleAspectFit;
- _productImgView.backgroundColor = [UIColor whiteColor];
- }
- return _productImgView;
- }
- - (UILabel *)descripLabel
- {
- if (_descripLabel == nil) {
- _descripLabel = [[UILabel alloc]init];
- _descripLabel.backgroundColor = [UIColor clearColor];
- _descripLabel.font = [UIFont systemFontOfSize:13];
- _descripLabel.textColor = UIColorFromRGB(0x333333);
- _descripLabel.numberOfLines = 2;
- _descripLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- }
- return _descripLabel;
- }
- - (UILabel *)priceLabel
- {
- if (_priceLabel == nil) {
- _priceLabel = [[UILabel alloc]init];
- _priceLabel.backgroundColor = [UIColor clearColor];
- _priceLabel.font = [UIFont systemFontOfSize:12];
- _priceLabel.textColor = UIColorFromRGB(0x333333);
- _priceLabel.textAlignment = NSTextAlignmentRight;
- [_priceLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
- }
- return _priceLabel;
- }
- - (UIButton *)addBtn{
- if (_addBtn == nil) {
- _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_addBtn setImage:[UIImage imageNamed:@"specification_body_add_n"] forState:UIControlStateNormal];
- [_addBtn addTarget:self action:@selector(numberChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- _addBtn.tag = 0;
- [_addBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
- }
- return _addBtn;
- }
- - (UIButton *)reduceBtn{
- if (_reduceBtn == nil) {
- _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_reduceBtn setImage:[UIImage imageNamed:@"specification_body_lower_n"] forState:UIControlStateNormal];
- [_reduceBtn addTarget:self action:@selector(numberChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- _reduceBtn.tag = 1;
- [_reduceBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
- [_reduceBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
- }
- return _reduceBtn;
- }
- - (UILabel *)specLabel{
- if (_specLabel == nil) {
- _specLabel = [[UILabel alloc]init];
- _specLabel.font = [UIFont systemFontOfSize:13];
- _specLabel.textColor = UIColorFromRGB(0x999999);
- _specLabel.numberOfLines = 0;
- _specLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- }
- return _specLabel;
- }
- - (UILabel *)numberLabel
- {
- if (_numberLabel == nil) {
- _numberLabel = [[UILabel alloc]init];
- _numberLabel.textColor = UIColorFromRGB(0x333333);
- _numberLabel.font = [UIFont systemFontOfSize:14];
- _numberLabel.textAlignment = NSTextAlignmentCenter;
- _numberLabel.layer.borderColor = [UIColorFromRGB(0x999999) CGColor];
- _numberLabel.layer.borderWidth = 0.5f;
- _numberLabel.text = @"1";
- }
- return _numberLabel;
- }
- - (UILabel *)groupNumberLabel
- {
- if (_groupNumberLabel == nil) {
- _groupNumberLabel = [[UILabel alloc]init];
- _groupNumberLabel.textColor = UIColorFromRGB(0x999999);
- _groupNumberLabel.font = [UIFont systemFontOfSize:12];
- _groupNumberLabel.textAlignment = NSTextAlignmentRight;
- _groupNumberLabel.hidden = YES;
- _groupNumberLabel.text = @"x1";
- }
- return _groupNumberLabel;
- }
- - (NSInteger )number
- {
- return self.numberLabel.text.integerValue >= 0 ? self.numberLabel.text.integerValue : 0;
- }
- - (void)setNumber:(NSInteger)number
- {
- if (number <= 0) {
- number = 1;
- }
- self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
- }
- @end
|