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
//
// KWMSearchFeedback.m
// iCemarose
//
// Created by 陈荣科 on 16/9/1.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMSearchFeedbackVC.h"
#import "KWMBrandCaramelCell.h"
#import "KWMSearchFeedBackView.h"
#import "KWMProductDetailVC.h"
@interface KWMSearchFeedbackVC ()<UITableViewDelegate,UITableViewDataSource,KWMSearchFeedBackViewDelegate,KWMBrandCaramelCellDelegate>
@property (nonatomic) KWMSearchFeedBackView *vSearchFB;
//@property (nonatomic) NSArray<KWMDataProduct *> *data;
@end
@implementation KWMSearchFeedbackVC{
BUYProduct *_product;
NSInteger endPage;
NSInteger total;
NSInteger mark;//标记展开标尺的pruduct下标
}
+(NSString *)kwmTag{
return @"KWMSearchFeedbackVC";
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[_vSearchFB removeFromSuperview];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
self.vBackground.hidden = YES;
[self initHeardView];
}
- (UITableView *)targetTableView{
return self.tbvSearch;
}
- (void)loadData{
[self getSearchProduct];
}
- (void)reLoadData{
[self getSearchProduct];
}
static NSString *idStr = @"KWMBrandCaramelCell";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// _data = [NSArray array];
total = 0;
mark = -1;
self.tbvSearch.delegate = self;
self.tbvSearch.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.tbvSearch registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
[self.vBackground addGestureRecognizer:tapGesture];
[self getSearchProduct];
}
- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
[_vSearchFB.tfSearch resignFirstResponder];
self.vBackground.hidden = YES;
}
- (void)initHeardView{
_vSearchFB = [[KWMSearchFeedBackView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
_vSearchFB.tfSearch.text = self.searchText;
_vSearchFB.lbGoodsNum.hidden = NO;
_vSearchFB.lbGoodsNum.text = [NSString stringWithFormat:@"%lu商品",(unsigned long)total];
_vSearchFB.delegate = self;
[self.view addSubview:_vSearchFB];
}
#pragma mark -- KWMBrandCaramelCellDelegate
//点击左边商品回调
- (void)kwm_onClickLeftIamge:(KWMBrandCaramelCell *)brandCell{
KWMProductDetailVC *productDetailVC = (KWMProductDetailVC*)[KWMBaseVC findControllerBy:[KWMProductDetailVC kwmTag] fromStoryboard:@"New"];
productDetailVC.productId = brandCell.leftDataProduct.id;
[self.navigationController pushViewController:productDetailVC animated:YES];
}
//点击右边商品回调
- (void)kwm_onClickRightIamge:(KWMBrandCaramelCell *)brandCell{
KWMProductDetailVC *productDetailVC = (KWMProductDetailVC*)[KWMBaseVC findControllerBy:[KWMProductDetailVC kwmTag] fromStoryboard:@"New"];
productDetailVC.productId = brandCell.rightDataProduct.id;
[self.navigationController pushViewController:productDetailVC animated:YES];
}
- (void)kwm_openRule:(BUYProduct *)product Or:(KWMDataProduct *)dataProduct And:(KWMBrandCaramelCell *)brandCell{
if (product != nil) {
[self.dataList enumerateObjectsUsingBlock:^(BUYProduct *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isEqual:product]) {
if (mark != idx) {
mark = idx;
[self.tbvSearch reloadData];
}
*stop = YES;
}
}];
}else if (dataProduct != nil){
[self.dataList enumerateObjectsUsingBlock:^(KWMDataProduct *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isEqual:dataProduct]) {
if (mark != idx) {
mark = idx;
[self.tbvSearch reloadData];
}
*stop = YES;
}
}];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (mark % 2 ) {
[brandCell openRule:NO];
}else{
[brandCell openRule:YES];
}
});
}
#pragma mark -- KWMSearchFeedBackViewDelegate
- (void)kwm_cancelSearch{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)kwm_tfReturnSearchFinished:(NSString *)text{
[self.dataList removeAllObjects];
self.searchText = text;
self.vBackground.hidden = YES;
[self getSearchProduct];
}
- (void)kwm_beginEditing{
self.vBackground.hidden = NO;
}
#warning -- 再次点击搜索
- (void)kwm_searchGoods{
NSLog(@"再次点击搜索");
}
#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count%2 ? self.dataList.count/2 + 1 : self.dataList.count/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
KWMBrandCaramelCell *brandCaramelCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
brandCaramelCell.delegate = self;
NSInteger position = indexPath.row * 2;
if(self.dataList!=nil && self.dataList.count>0){
KWMDataProduct *leftDataProduct = [self.dataList objectAtIndex:position];
KWMDataProduct *rightDataProduct = self.dataList.count>(position+1)?[self.dataList objectAtIndex:(position+1)]:nil;
[brandCaramelCell setDataProduct:leftDataProduct rightProduct:rightDataProduct];
}
return brandCaramelCell;
}
#pragma mark -- UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 244;
}
//搜索API
- (void)getSearchProduct{
if (self.currentPage.integerValue == endPage && endPage != 0) {
[self showToast:@"已经是最后一页"];
[self hideLoading];
return;
}
NSInteger tagetPage = self.currentPage.integerValue + 1;
NSDictionary *parameters = @{
@"keyword":_searchText,
@"page":@(tagetPage).stringValue,
};
__weak KWMSearchFeedbackVC *weakSelf = self;
[self.api searchProduct:parameters success:^(NSURLSessionDataTask *task, KWMSearchResult *result) {
[weakSelf hideLoading];
total = result.products.total.integerValue;
endPage = result.products.lastPage.integerValue;
[weakSelf appendDataList:result.productList setPage:tagetPage];
_vSearchFB.lbGoodsNum.text = [NSString stringWithFormat:@"%lu商品",(unsigned long)total];
[self.tbvSearch reloadData];
if (self.dataList.count == 0) {
self.vNoGoods.hidden = NO;
}else{
self.vNoGoods.hidden = YES;
}
_vSearchFB.lbGoodsNum.hidden = NO;
} failure:^(NSURLSessionDataTask *task, NSError *error) {
_vSearchFB.lbGoodsNum.hidden = NO;
[weakSelf showError:error];
NSLog(@"error = %@",error);
}];
}
@end