KWMSearchBrandVC.m 12.2 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
//  KWMSearchBrandVC.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/30.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSearchBrandVC.h"
#import "NSString+PinYin.h"
#import "KWMSearchBrandView.h"
#import "KWMStringUtil.h"
#import "KWMBrandCaramelVC.h"
#import "KWMSearchBrandsCell.h"
u  
lee committed
15
#import <RXCollections/RXCollection.h>
houweibin committed
16 17 18 19 20



@interface KWMSearchBrandVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,KWMSearchBrandViewDelegate>
@property (nonatomic,strong)  NSMutableArray  *dataArray;
21
@property (nonatomic,weak)KWMSearchBrandView *searchBrandView;
houweibin committed
22 23
@end

24
@implementation KWMSearchBrandVC {
houweibin committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    NSMutableArray *searchArr;
    NSString *searchTitle;
}

+(NSString *)kwmTag{
    return @"KWMSearchBrandVC";
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
44
    [self initHeaderView];
houweibin committed
45 46 47 48 49 50
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"所有品牌";
    self.vLine.backgroundColor = [UIColor colorWithRed:244.0/255 green:245.0/255 blue:247.0/255 alpha:1];
    searchArr = [NSMutableArray array];
    [self initTableView];
lee committed
51
    [self loadStaticData];
houweibin committed
52 53 54
    [self RequestBrandsAPI];
}

lee committed
55 56 57 58 59 60 61 62 63 64
- (void) loadStaticData {
    NSArray *brands = @[@"Amelie Wang",@"Babywalker",@"Beau Loves",@"Billieblush",@"Billybandit",@"Bonpoint",@"Burberry",@"Caramel",@"Chloé",@"Dolce & Gabbana",@"Emile et Ida",@"Fendi",@"Givenchy",@"Gucci",@"Hucklebones",@"Il Gufo",@"Indikidual",@"Karl Lagerfeld Kids",@"Kenzo",@"Lanvin",@"Little Marc Jacobs",@"Loud Apparel",@"Louis Louise",@"Louise Misha",@"Marie-Chantal",@"Marni",@"Mini Melissa",@"Mini Rodini",@"Mischka Aoki",@"Moncler",@"MOSCHINO",@"MSGM",@"Nailmatic",@"Nana Huchy",@"Noukie's",@"Oeuf",@"Patachou",@"Paul Smith",@"PèPè",@"Sons + Daughters Eyewear",@"Stella McCartney Kids",@"Story Loris",@"Tartine et Chocolat",@"The Animals Observatory",@"Tiny Cottons",@"Tip Toey Joey",@"Veja",@"Wolf & Rita",@"Young Versace",@"Zadig & Voltaire"];
    
    NSArray  *indexArray= [[brands rx_filterWithBlock:^BOOL(id each) {
        return each && each != [NSNull null];
    }] arrayWithPinYinFirstLetterFormat];
    _dataArray =[NSMutableArray arrayWithArray:indexArray];
    [self.tbvSearchBrand reloadData];
}

houweibin committed
65
- (void)initHeaderView{
66 67 68
    KWMSearchBrandView *searchBrandView = [[KWMSearchBrandView alloc] init];
    [self.view addSubview:searchBrandView];
    self.searchBrandView = searchBrandView;
houweibin committed
69 70
    self.searchBrandView.delegate = self;
    [self initCustomTitleBarConstarints:self.searchBrandView];
houweibin committed
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

static NSString *identify = @"KWMSearchBrandsCell";

- (void)initTableView{
    self.tbvSearchBrand.sectionIndexColor = [UIColor blackColor];
    self.tbvSearchBrand.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.tbvSearchBrand setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tbvSearchBrand registerNib:[UINib nibWithNibName:identify bundle:nil] forCellReuseIdentifier:identify];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
    [self.vBackground addGestureRecognizer:tapGesture];
}

- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
    
    [_searchBrandView CancelSearch];
    self.vBackground.hidden = YES;
}

#pragma merk -- KWMSearchBrandViewDelegate
- (void)kwm_GoBackLastController{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)kwm_cancelSearch{
    [searchArr removeAllObjects];
    searchTitle = nil;
    self.vBackground.hidden = YES;
    [self.tbvSearchBrand reloadData];
}

- (void)kwm_tfSearchBeginEdit{
    self.vBackground.hidden = NO;
}

- (void)kwm_InputSpace:(NSString *)text{
    if (text.length == 0 || text == nil) {
        [self showToast:@"请输入需要搜索的内容"];
    }else{
        [_searchBrandView.tfBrand resignFirstResponder];
        self.vBackground.hidden = YES;
        [self setSearchBrandsWith:text];
    }
}

- (void)kwm_tfValueChange:(NSString *)text{

    if (text.length==0) {
        [searchArr removeAllObjects];
        searchTitle = nil;
        [self.tbvSearchBrand reloadData];
        return;
    }
    [self setSearchBrandsWith:text];
}

//搜索品牌名字判断
- (void)setSearchBrandsWith:(NSString *)text{
    
    //首字母大写
    NSString *pinYinStr = [KWMStringUtil transform:text];
    pinYinStr = [pinYinStr stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[pinYinStr substringToIndex:1] uppercaseString]];
    unichar aChar = [pinYinStr characterAtIndex:0];
    NSString *firstStr = [NSString stringWithFormat:@"%c",aChar];
    
    
    [self searchBrand:pinYinStr AndFirst:firstStr];
    [self.tbvSearchBrand reloadData];
}


- (void)searchBrand:(NSString *)brandStr AndFirst:(NSString *)firstStr{
    
    BOOL isOne = NO;
    if ([firstStr isEqualToString:[brandStr uppercaseString]]) {
        isOne = YES;
    }
    for (int i = 0; i < _dataArray.count; ++i) {
        NSDictionary *dict = self.dataArray[i];
        if (firstStr != nil) {
            searchTitle = firstStr;
            if ([firstStr isEqualToString:dict[@"firstLetter"]]) {
                [self JumpArr:dict[@"content"] Has:brandStr WithOnlyOne:isOne];
            }
        }
    }
}

- (void)JumpArr:(NSArray *)arr Has:(NSString *)str WithOnlyOne:(BOOL)isOne{
    
    if (searchArr.count > 0) {
        [searchArr removeAllObjects];
    }
    if (isOne) {//输入第一个字母的时候
        [searchArr addObjectsFromArray:arr];
        return ;
    }
    for (int i = 0; i < arr.count; ++i) {

        NSString *tempStr = arr[i];
        if ([tempStr containsString:str]) {
            [searchArr addObject:tempStr];
        }
    }
}

#pragma mark--- UITableViewDataSource and UITableViewDelegate Methods---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (searchArr.count > 0 || searchTitle != nil) {
        return 1;
    }
    return [self.dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (searchArr.count > 0 || searchTitle != nil) {
        return searchArr.count;
    }
    NSDictionary *dict = self.dataArray[section];
    NSMutableArray *array = dict[@"content"];
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWMSearchBrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (searchArr.count > 0 || searchTitle != nil) {
        cell.lbBrand.text = searchArr[indexPath.row];
    }else{
        NSDictionary *dict = self.dataArray[indexPath.section];
        NSMutableArray *array = dict[@"content"];
        cell.lbBrand.text = array[indexPath.row];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //自定义Header标题
    UIView* myView = [[UIView alloc] init];
    myView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 90, 40)];
    titleLabel.textColor=[UIColor blackColor];
    NSString *title;
    if (searchTitle != nil) {
        title = searchTitle;
    }else{
       title = self.dataArray[section][@"firstLetter"];
    }
    titleLabel.text=title;
    titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:24];
    UIView *vLine = [[UIView alloc] initWithFrame:CGRectMake(20, 39, UI_SCREEN_WIDTH - 40, 1)];
    vLine.backgroundColor = [UIColor colorWithRed:244.0/255 green:245.0/255 blue:247.0/255 alpha:1];
    [myView addSubview:vLine];
    [myView addSubview:titleLabel];
    
    return myView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH ,5)];
    footerView.backgroundColor = [UIColor colorWithRed:244.0/255 green:245.0/255 blue:247.0/255 alpha:1];
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *brand;
    if (searchArr.count > 0) {
        brand = searchArr[indexPath.row];
    }else{
        NSDictionary *dict = self.dataArray[indexPath.section];
        brand = [dict[@"content"] objectAtIndex:indexPath.row];
    }
lee committed
249 250
    NSString *str = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brand];
//    NSString *str = brand;
houweibin committed
251
    
lee committed
252
    NSLog(@"str: %@",str);
lee committed
253 254 255 256 257 258 259 260
//    //品牌介绍
//    KWMBrandCaramelVC *brandCaramelVC = (KWMBrandCaramelVC *)[KWMBaseVC findControllerBy:[KWMBrandCaramelVC kwmTag] fromStoryboard:@"Brand"];
//    brandCaramelVC.handle = str;
//    brandCaramelVC.brand = brand;
//    brandCaramelVC.isSale = NO;
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    [self.navigationController pushViewController:brandCaramelVC animated:YES];
    [self openURLWithString:[NSString stringWithFormat:@"https://www.cemarose.com/collections/%@",str]];
houweibin committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

#pragma mark---tableView索引相关设置----
//添加TableView头视图标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict = self.dataArray[section];
    NSString *title = dict[@"firstLetter"];
    return title;
}

//添加索引栏标题数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //搜索品牌时
    if (searchArr.count >0 || searchTitle != nil) {
        return nil;
    }
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSDictionary *dict in self.dataArray) {
        NSString *title = dict[@"firstLetter"];
        [resultArray addObject:title];
    }
    
    return resultArray;
}

//点击索引栏标题时执行
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //这里是为了指定索引index对应的是哪个section的,默认的话直接返回index就好。其他需要定制的就针对性处理
    if ([title isEqualToString:UITableViewIndexSearch])
    {
        [tableView setContentOffset:CGPointZero animated:NO];//tabview移至顶部
        return NSNotFound;
    }
    else
    {
        return [[UILocalizedIndexedCollation currentCollation]sectionForSectionIndexTitleAtIndex:index+1] - 1; // -1 添加了搜索标识
    }
}

- (void)RequestBrandsAPI{
    __weak KWMSearchBrandVC *weakSelf = self;
    void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
lee committed
306 307
//        [weakSelf hideLoading];
//        [weakSelf showError:error];
houweibin committed
308 309
    };
    void(^success)(NSURLSessionDataTask *,KWMBrandsResult *) = ^(NSURLSessionDataTask *task,KWMBrandsResult *result){
lee committed
310
//        [weakSelf hideLoading];
houweibin committed
311 312 313
        if([weakSelf hasCemaroseError:result]){
            return ;
        }
u  
lee committed
314
//        NSArray  *indexArray= [result.brands arrayWithPinYinFirstLetterFormat];
315 316 317
//        if(self.dataArray){ //如果已经从缓存拿到数据了。
//            return;
//        }
houweibin committed
318 319 320 321 322 323 324
        if(result.brands){
            NSArray  *indexArray= [[result.brands rx_filterWithBlock:^BOOL(id each) {
                return each && each != [NSNull null];
            }] arrayWithPinYinFirstLetterFormat];
            _dataArray =[NSMutableArray arrayWithArray:indexArray];
            [weakSelf.tbvSearchBrand reloadData];
        }
houweibin committed
325
    };
326
    void(^cacheCallback)(KWMBrandsResult *) = ^(KWMBrandsResult *result){
houweibin committed
327 328 329 330 331 332 333 334 335
//        if([weakSelf hasCemaroseError:result]){
//            return ;
//        }
        if(result && result.brands){
            NSArray *indexArray= [[result.brands rx_filterWithBlock:^BOOL(id each) {
                return each && each != [NSNull null];
            }] arrayWithPinYinFirstLetterFormat];
            _dataArray =[NSMutableArray arrayWithArray:indexArray];
            [weakSelf.tbvSearchBrand reloadData];
336 337 338
        }
    };
    [self.api getAllBrand:nil cacheCallback:cacheCallback success:success failure:failure];
lee committed
339
//    [self showLoading];
houweibin committed
340 341 342
}

@end