IVTMeSearchViewController.m
8.16 KB
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
//
// IVTMeSearchViewController.m
// IVTMeLiveTwo
//
// Created by XRG on 16/8/25.
// Copyright © 2016年 QLQ. All rights reserved.
//
#import "IVTMeSearchViewController.h"
#import "IVTMyFollowListTableViewCell.h"
#import "IVTUserInfoCenterViewController.h"
@interface IVTMeSearchViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
UIView *searchView;
UITextField *textField;
NSMutableArray *requestDataArray;
UITableView *myTableView;
NSString *keywordString;
UILabel *noDataLabel;
}
@end
@implementation IVTMeSearchViewController
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self setSearchView];
}
- (void)viewDidLoad {
[super viewDidLoad];
requestDataArray = [[NSMutableArray alloc]initWithCapacity:10];
[self setNavigationBar];
[self setShowView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doChangeFollowStatus) name:@"followedChanged" object:nil];
noDataLabel = [[UILabel alloc]init];
noDataLabel.font = [UIFont systemFontOfSize:13];
noDataLabel.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
noDataLabel.text = @"没有数据";
noDataLabel.hidden = NO;
noDataLabel.font = [UIFont systemFontOfSize:16];
noDataLabel.text = @"空空如也~~";
noDataLabel.textAlignment = 1;
[self.view addSubview:noDataLabel];
}
- (void)setNavigationBar{
[self hiddenLeftBarBackButtonItem];
}
- (void)setSearchView{
searchView = [[UIView alloc]init];
searchView.frame = CGRectMake(0, 0, kScreenWidth, 44);
searchView.backgroundColor = [UIColor clearColor];
UIView *textBgView = [[UIView alloc]initWithFrame:CGRectMake(20, 7, kScreenWidth - 70, 30)];
textBgView.backgroundColor = [UIColor whiteColor];
textBgView.layer.cornerRadius = 5;
textBgView.clipsToBounds = YES;
[searchView addSubview:textBgView];
UIImageView *searchIcon = [[UIImageView alloc]initWithFrame:CGRectMake(8, 8, CGRectGetHeight(textBgView.frame) - 16, CGRectGetHeight(textBgView.frame) - 16)];
searchIcon.image = [UIImage imageNamed:@"输入框中搜索"];
[textBgView addSubview:searchIcon];
textField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetWidth(searchIcon.frame) + 10, 0, CGRectGetWidth(textBgView.frame) - CGRectGetWidth(searchIcon.frame) + 10, CGRectGetHeight(textBgView.frame))];
textField.borderStyle = UITextBorderStyleNone;
textField.font = [UIFont systemFontOfSize:15];
textField.returnKeyType = UIReturnKeySearch;
textField.placeholder = @"输入蜜家号或昵称";
[textField addTarget:self action:@selector(searchTextChanged) forControlEvents:UIControlEventEditingChanged];
[textField addTarget:self action:@selector(searchTextDone:) forControlEvents:(UIControlEventEditingDidEndOnExit)];
[textField addTarget:self action:@selector(searchTextBegin:) forControlEvents:(UIControlEventEditingDidBegin)];
[textField becomeFirstResponder];
[textBgView addSubview:textField];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(kScreenWidth - 50, 0, 50, 44);
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(doCancel) forControlEvents:UIControlEventTouchUpInside];
[searchView addSubview:cancelButton];
[self.navigationController.navigationBar addSubview:searchView];
}
- (void)searchTextChanged{
if (textField.markedTextRange != nil) {
return;
}
keywordString = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (keywordString.length>0) {
noDataLabel.hidden = YES;
myTableView.hidden = NO;
[self doRequest];
}
else{
myTableView.hidden = YES;
noDataLabel.hidden = NO;
}
}
-(void)searchTextDone:(UITextField*)sender
{
// [self doRequest];
}
-(void)searchTextBegin:(UITextField*)sender
{
[self adjustUIWithText:sender.text hinting:NO];
}
-(void)adjustUIWithText:(NSString*)text hinting:(BOOL)hinting
{
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (text.length>0) {
[self setShowView];
// [self doRequest];
myTableView.hidden = NO;
}else {
myTableView.hidden = YES;
}
}
- (void)doCancel{
[textField endEditing:YES];
[searchView removeFromSuperview];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)setShowView{
__weak __typeof(self)weakSelf = self;
myTableView = [[UITableView alloc] init];
myTableView.backgroundColor = [UIColor IVTBackgroundColor];
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.showsHorizontalScrollIndicator = NO;
myTableView.showsVerticalScrollIndicator = NO;
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
myTableView.tableFooterView = [UIView new];
[self.view addSubview:myTableView];
[myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.view.mas_left);
make.top.equalTo(weakSelf.view.mas_top);
make.right.equalTo(weakSelf.view.mas_right);
make.bottom.equalTo(weakSelf.view.mas_bottom);
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[textField endEditing:YES];
}
#pragma --mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return requestDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
IVTMyFollowListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[IVTMyFollowListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.entity = [requestDataArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 59;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[textField endEditing:YES];
[searchView removeFromSuperview];
IVTUserInfoCenterViewController *userInfoCenterVC = [[IVTUserInfoCenterViewController alloc]init];
userInfoCenterVC.userId = [NSString stringWithFormat:@"%ld",[[[requestDataArray objectAtIndex:indexPath.row] valueForKey:@"id"] integerValue]];
[self.navigationController pushViewController:userInfoCenterVC animated:YES];
}
- (void)doRequest{
if ([keywordString length] == 0) {
return;
}
[[IVTNetworkAPIClient sharedClient] getSearchResultListWithKeyword:keywordString success:^(NSDictionary *successData) {
[requestDataArray removeAllObjects];
[requestDataArray addObjectsFromArray:successData[@"data"]];
if (requestDataArray.count == 0) {
noDataLabel.hidden = NO;
}
else{
noDataLabel.hidden = YES;
}
[myTableView reloadData];
} error:^(NSDictionary *successData) {
} failure:^(NSError *failureError) {
}];
}
- (void)doChangeFollowStatus{
[myTableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end