KWMSecondDetailView.m 6.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//
//  KWMSecondDetailView.m
//  iCemarose
//
//  Created by 陈荣科 on 16/9/6.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSecondDetailView.h"
#import "KWMTBVSectionHeardView.h"
11
#import "KWMProductSameBrandView.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

@interface KWMSecondDetailView ()

@property(nonatomic,weak) IBOutlet UIView *vView;

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

@property(nonatomic,weak) IBOutlet UIPageControl *pageControl;

@end

@implementation KWMSecondDetailView

#define sclHeight (UI_SCREEN_HEITHT-20-20-40-90-64)
static NSString *idStr = @"KWMNewGoodsCell";
- (id)init{
    if (self=[super init]){
        [self addView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self addView];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void) addView{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil];
    
    self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.vView];
    [self initView];
    

}


- (void)initView{
    self.isLoadingSameBrand = NO;
    self.isLoadingSameType = NO;
    [self.tableView registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
}

- (void)setCount:(NSInteger)count{
    _count = count+1;
    if(count>0){
        self.pageControl.numberOfPages = count;
        //需要放在设置图片之前,不然会发生crash
        self.pageControl.currentPage = self.count-1;

        [self.pageControl setValue:[UIImage imageNamed:@"ic_page_3"] forKey:@"_currentPageImage"];
        [self.pageControl setValue:[UIImage imageNamed:@"ic_page_1"] forKey:@"_pageImage"];
    }
}


#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWMNewGoodsCell *newGoodsCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
    if(!newGoodsCell){
        [tableView registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
        newGoodsCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
    }
    newGoodsCell.isDetail = YES;
    newGoodsCell.delegate = self;
    newGoodsCell.isSameBrand = indexPath.section == 0;
    if(newGoodsCell.isSameBrand){//同品牌推荐
        if(self.pageSameBrand <= 1){
            [newGoodsCell setData:self.sameBrandProducts];
        }else{
            [newGoodsCell addData:self.sameBrandProducts];
        }
    }else{//同类型商品推荐
        if(self.pageSameType <= 1){
            [newGoodsCell setData:self.sameTypeProducts];
        }else{
            [newGoodsCell addData:self.sameTypeProducts];
        }
    }
    return newGoodsCell;
}

#pragma mark -- KWMNewGoodsCellDelegate
- (void)kwm_selectCollectionCell:(BUYProduct *)product{
    if ([self.delegate respondsToSelector:@selector(kwm_clickCollectionCell:)]) {
        [self.delegate kwm_clickCollectionCell:(BUYProduct *)product];
    }
}

houweibin committed
130 131 132
-(void)kwm_onClickMore:(BOOL)isNewBrand{
    if ([self.delegate respondsToSelector:@selector(kwm_clickMore:)]) {
        [self.delegate kwm_clickMore:isNewBrand];
133 134 135 136 137 138 139 140 141 142
    }
}

#pragma mark - UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  173.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
143 144 145 146 147 148
        KWMProductSameBrandView *view = [[[NSBundle mainBundle] loadNibNamed:@"KWMProductSameBrandView" owner:nil options:nil] firstObject];
        view.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, [self tableView:tableView heightForHeaderInSection:section]);
        view.brandName.text = self.brand ?: @"其他商品";
        view.quantity.text = self.productCount.stringValue ?: @"0";
        view.brandDescription.text = self.brandDescription;
        return view;
149
    }else{
150
        KWMTBVSectionHeardView *vTBVSectionHeard = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 60)];
151 152
        vTBVSectionHeard = [[KWMTBVSectionHeardView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
        vTBVSectionHeard.lbSectionTitel.text = @"同类商品";
153 154
        vTBVSectionHeard.ivMore.hidden = YES;
        return vTBVSectionHeard;
155 156 157 158 159
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0 && self.brand != nil){
160 161 162 163 164 165
        CGFloat height = 8;
//        height += [self.brandDescription sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]}].height;
        height += [self.brandDescription boundingRectWithSize:CGSizeMake(UI_SCREEN_WIDTH - 54, 0) options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:11]} context:nil].size.height;
        [self.delegate kwm_updatedSecondDetailViewHeight: 450 + height];
        return 60 + height;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }else{
        return 40;
    }
}


-(void)setData:(NSArray *)products isSameBrand:(BOOL)isSameBrand{
    if(products == nil || products.count == 0){
        return;
    }
    if(isSameBrand){
        self.sameBrandProducts = products;
    }else{
        self.sameTypeProducts = products;
    }
    [self.tableView reloadData];
}

-(void)setBrandString:(NSString *)brand{
    if(brand == nil){
        return;
    }
    self.brand = brand;
}

-(void)clear{
    self.brand = @"";
    self.pageSameBrand = 0;
    self.pageSameType = 0;
    self.sameTypeProducts = nil;
    self.sameBrandProducts = nil;
    [self.tableView reloadData];
}



@end