1
2
3
4
5
6
7
8
9
10
11
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
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
249
250
251
252
253
254
255
256
257
258
259
260
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
306
307
308
309
310
311
312
313
//
// 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"
#import <RXCollections/RXCollection.h>
@interface KWMSearchBrandVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,KWMSearchBrandViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic)KWMSearchBrandView *searchBrandView;
@end
@implementation KWMSearchBrandVC {
NSMutableArray *searchArr;
NSString *searchTitle;
}
+(NSString *)kwmTag{
return @"KWMSearchBrandVC";
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[_searchBrandView removeFromSuperview];
searchArr = nil;
searchTitle = nil;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
[self initHeaderView];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 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];
[self RequestBrandsAPI];
}
- (void)initHeaderView{
_searchBrandView = [[KWMSearchBrandView alloc] initWithFrame:CGRectMake(0, 20, UI_SCREEN_WIDTH, 44)];
[self.view addSubview:_searchBrandView];
_searchBrandView.delegate = self;
}
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];
}
NSString *str = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brand];
NSLog(@"str: %@",str);
//品牌介绍
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];
}
#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){
[weakSelf hideLoading];
[weakSelf showError:error];
};
void(^success)(NSURLSessionDataTask *,KWMBrandsResult *) = ^(NSURLSessionDataTask *task,KWMBrandsResult *result){
[weakSelf hideLoading];
if([weakSelf hasCemaroseError:result]){
return ;
}
// NSArray *indexArray= [result.brands arrayWithPinYinFirstLetterFormat];
NSArray *indexArray= [[result.brands rx_filterWithBlock:^BOOL(id each) {
return each && each != [NSNull null];
}] arrayWithPinYinFirstLetterFormat];
_dataArray =[NSMutableArray arrayWithArray:indexArray];
[weakSelf.tbvSearchBrand reloadData];
};
[self.api getAllBrand:nil success:success failure:failure];
[self showLoading];
}
@end