Skip to content

Commit 20ff606

Browse files
committed
提交第一阶段搜索页面代码
1 parent 699c6fd commit 20ff606

8 files changed

Lines changed: 379 additions & 99 deletions

File tree

Coding_iOS/Controllers/Search/CSSearchDisplayVC.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <UIKit/UIKit.h>
1010

11-
@interface CSSearchDisplayVC : UISearchDisplayController
11+
@interface CSSearchDisplayVC : UISearchDisplayController {
1212

13+
}
1314
@end

Coding_iOS/Controllers/Search/CSSearchDisplayVC.m

Lines changed: 243 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,35 @@
88

99
#import "CSSearchDisplayVC.h"
1010

11-
#import <QuartzCore/QuartzCore.h>
1211
#import "TopicHotkeyView.h"
1312
#import "Coding_NetAPIManager.h"
13+
#import "ODRefreshControl.h"
14+
#import "SVPullToRefresh.h"
1415

15-
@interface CSSearchDisplayVC ()
16+
#import "CSSearchModel.h"
1617

17-
@property (nonatomic, strong) UIView *contentView;
18+
#define kCellIdentifier_Search @"com.coding.search.tweet.result"
19+
20+
@interface CSSearchDisplayVC () <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
1821

22+
@property (nonatomic, strong) UIView *contentView;
1923
@property (nonatomic, strong) UIButton *btnMore;
24+
@property (nonatomic, strong) TopicHotkeyView *topicHotkeyView;
2025

21-
- (void)initSubViewsInContentView;
26+
@property (nonatomic, strong) UITableView *searchTableView;
27+
@property (nonatomic, strong) ODRefreshControl *refreshControl;
28+
@property (nonatomic, strong) NSMutableArray *tweetsArr;
29+
@property (nonatomic, assign) NSInteger totalPage;
30+
@property (nonatomic, assign) NSInteger currentPage;
31+
@property (nonatomic, assign) BOOL isLoading;
2232

23-
- (void)didClickedMoreHotkey:(id)sender;
33+
@property (nonatomic, strong) UIScrollView *searchHistoryView;
2434

35+
- (void)initSubViewsInContentView;
36+
- (void)initSearchResultsTableView;
37+
- (void)initSearchHistoryView;
38+
- (void)didClickedMoreHotkey:(id)sender;
39+
- (void)didCLickedCleanSearchHistory:(id)sender;
2540
@end
2641

2742
@implementation CSSearchDisplayVC
@@ -33,6 +48,7 @@ - (void)setActive:(BOOL)visible animated:(BOOL)animated {
3348
if(_contentView) {
3449

3550
[_contentView removeFromSuperview];
51+
[_searchTableView removeFromSuperview];
3652
[super setActive:visible animated:animated];
3753
}
3854
}else {
@@ -49,23 +65,24 @@ - (void)setActive:(BOOL)visible animated:(BOOL)animated {
4965
((UIView*)sub[2]).hidden = YES;
5066
}
5167
}
52-
53-
if(!_contentView) {
54-
55-
_contentView = [[UIView alloc] init];
56-
_contentView.frame = CGRectMake(0.0f, 60.0f, kScreen_Width, kScreen_Height - 60.0f);
57-
_contentView.backgroundColor = [UIColor whiteColor];
58-
59-
[self initSubViewsInContentView];
60-
}
61-
62-
[self.searchBar.superview addSubview:_contentView];
63-
[self.searchBar.superview bringSubviewToFront:_contentView];
64-
6568
} else {
66-
69+
6770
[[subViews lastObject] removeFromSuperview];
6871
}
72+
73+
if(!_contentView) {
74+
75+
_contentView = [[UIView alloc] init];
76+
_contentView.frame = CGRectMake(0.0f, 60.0f, kScreen_Width, kScreen_Height - 60.0f);
77+
_contentView.backgroundColor = [UIColor whiteColor];
78+
79+
[self initSubViewsInContentView];
80+
}
81+
82+
[self.searchBar.superview addSubview:_contentView];
83+
[self.searchBar.superview bringSubviewToFront:_contentView];
84+
__weak typeof(self) weakSelf = self;
85+
self.searchBar.delegate = weakSelf;
6986
}
7087
}
7188

@@ -75,7 +92,6 @@ - (void)setActive:(BOOL)visible animated:(BOOL)animated {
7592
- (void)initSubViewsInContentView {
7693

7794
UILabel *lblHotKey = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 5.0f, 100, 30.0f)];
78-
// lblHotKey.backgroundColor = [UIColor redColor];
7995
[lblHotKey setText:@"热门话题"];
8096
[lblHotKey setFont:[UIFont systemFontOfSize:14.0f]];
8197
[lblHotKey setTextColor:[UIColor colorWithHexString:@"0x999999"]];
@@ -88,21 +104,19 @@ - (void)initSubViewsInContentView {
88104
[_btnMore addTarget:self action:@selector(didClickedMoreHotkey:) forControlEvents:UIControlEventTouchUpInside];
89105
[_contentView addSubview:_btnMore];
90106

91-
// UILabel *lblTopic = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 30.0f, 160.0f, 25.0f)];
92-
// [lblTopic setText:@" #我最爱的编程语言# "];
93-
// [lblTopic setFont:[UIFont systemFontOfSize:14.0f]];
94-
// [lblTopic setTextColor:[UIColor colorWithHexString:@"0x3bbd79"]];
95-
// lblTopic.layer.borderColor = [[UIColor colorWithHexString:@"0x3bbd79"] CGColor];
96-
// lblTopic.layer.borderWidth = 1.0f;
97-
// lblTopic.layer.cornerRadius = 13.0f;
98-
// [_contentView addSubview:lblTopic];
99-
100-
// TopicItemView *itemView = [[TopicItemView alloc] initWithTopic:@"#Coding#" Color:[UIColor colorWithHexString:@"0xb5b5b5"]];
101-
// itemView.frame = CGRectMake(12.0f, 50.0f, 100.0, 100.0f);
102-
// [_contentView addSubview:itemView];
107+
_topicHotkeyView = [[TopicHotkeyView alloc] init];
108+
[_contentView addSubview:_topicHotkeyView];
109+
[_topicHotkeyView mas_makeConstraints:^(MASConstraintMaker *make) {
110+
111+
make.left.mas_equalTo(@0);
112+
make.top.mas_equalTo(@40);
113+
make.width.mas_equalTo(kScreen_Width);
114+
make.height.mas_equalTo(@0);
115+
}];
103116

104-
__weak typeof(_contentView) __contentView = _contentView;
117+
[self initSearchHistoryView];
105118

119+
__weak typeof(self) weakSelf = self;
106120
[[Coding_NetAPIManager sharedManager] request_TopicHotkeyWithBlock:^(id data, NSError *error) {
107121
if(data) {
108122
NSArray *array = data;
@@ -111,15 +125,209 @@ - (void)initSubViewsInContentView {
111125
[hotkeyArray addObject:[(NSDictionary *)array[i] objectForKey:@"name"]];
112126
}
113127

114-
TopicHotkeyView *test = [[TopicHotkeyView alloc] initWithHotkeys:hotkeyArray withFrame:CGRectMake(0.0f, 40.0f, 160.0f, 25.0f)];
115-
[__contentView addSubview:test];
128+
[weakSelf.topicHotkeyView setHotkeys:hotkeyArray];
129+
[weakSelf.topicHotkeyView mas_remakeConstraints:^(MASConstraintMaker *make) {
130+
131+
make.left.mas_equalTo(@0);
132+
make.top.mas_equalTo(@40);
133+
make.width.mas_equalTo(kScreen_Width);
134+
make.height.mas_equalTo(weakSelf.topicHotkeyView.frame.size.height);
135+
}];
136+
// [weakSelf.searchHistoryView setFrame:CGRectMake(weakSelf.searchHistoryView.frame.origin.x, weakSelf.topicHotkeyView.frame.origin.y + weakSelf.topicHotkeyView.frame.size.height,
137+
// weakSelf.searchHistoryView.frame.size.width, weakSelf.searchHistoryView.frame.size.height)];
116138
}
117139
}];
118140
}
119141

142+
- (void)initSearchResultsTableView {
143+
144+
_tweetsArr = [[NSMutableArray alloc] init];
145+
_currentPage = 1;
146+
147+
_searchTableView = ({
148+
149+
UITableView *tableView = [[UITableView alloc] initWithFrame:_contentView.frame style:UITableViewStylePlain];
150+
tableView.backgroundColor = [UIColor whiteColor];
151+
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
152+
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier_Search];
153+
tableView.dataSource = self;
154+
tableView.delegate = self;
155+
{
156+
__weak typeof(self) weakSelf = self;
157+
[tableView addInfiniteScrollingWithActionHandler:^{
158+
[weakSelf loadMore];
159+
}];
160+
}
161+
162+
[self.searchBar.superview addSubview:tableView];
163+
[self.searchBar.superview bringSubviewToFront:tableView];
164+
tableView;
165+
});
166+
167+
// _refreshControl = [[ODRefreshControl alloc] initInScrollView:self.searchTableView];
168+
// [_refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
169+
170+
[self refresh];
171+
}
172+
173+
- (void)initSearchHistoryView {
174+
175+
if(!_searchHistoryView) {
176+
177+
_searchHistoryView = [[UIScrollView alloc] init];
178+
_searchHistoryView.backgroundColor = [UIColor clearColor];
179+
[_contentView addSubview:_searchHistoryView];
180+
181+
[_searchHistoryView mas_makeConstraints:^(MASConstraintMaker *make) {
182+
183+
make.top.mas_equalTo(_topicHotkeyView.mas_bottom);
184+
make.left.mas_equalTo(@0);
185+
make.width.mas_equalTo(@320);
186+
make.height.mas_equalTo(@280);
187+
}];
188+
}
189+
190+
[[_searchHistoryView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
191+
192+
NSArray *array = [CSSearchModel getSearchHistory];
193+
CGFloat imageLeft = 12.0f;
194+
CGFloat textLeft = 32.0f;
195+
CGFloat height = 35.0f;
196+
UILabel *lblHistory = nil;
197+
UIImageView *imageView = nil;
198+
UIImage *image = [UIImage imageNamed:@"time_clock_icon"];
199+
200+
for (int i = 0; i < array.count; i++) {
201+
202+
lblHistory = [[UILabel alloc] initWithFrame:CGRectMake(textLeft, i * height, kScreen_Width - 2 * textLeft, height)];
203+
lblHistory.textColor = [UIColor colorWithHexString:@"0x999999"];
204+
lblHistory.text = array[i];
205+
206+
imageView = [[UIImageView alloc] initWithImage:image];
207+
imageView.frame = CGRectMake(imageLeft, i * height + (35 - image.size.height) / 2 + 2, image.size.width, image.size.height);
208+
209+
[_searchHistoryView addSubview:lblHistory];
210+
[_searchHistoryView addSubview:imageView];
211+
lblHistory = nil;
212+
imageView = nil;
213+
}
214+
215+
if(array.count) {
216+
217+
UIButton *btnClean = [UIButton buttonWithType:UIButtonTypeCustom];
218+
[btnClean setTitle:@"清除搜索历史" forState:UIControlStateNormal];
219+
[btnClean setTitleColor:[UIColor colorWithHexString:@"0x3bbd79"] forState:UIControlStateNormal];
220+
[btnClean setTitleColor:[UIColor colorWithHexString:@"0x3bbd79" andAlpha:.3] forState:UIControlStateHighlighted];
221+
[btnClean setFrame:CGRectMake(0, array.count * height, kScreen_Width, height)];
222+
[_searchHistoryView addSubview:btnClean];
223+
[btnClean addTarget:self action:@selector(didCLickedCleanSearchHistory:) forControlEvents:UIControlEventTouchUpInside];
224+
}
225+
226+
}
227+
120228
- (void)didClickedMoreHotkey:(id)sender {
121229

122230

231+
}
232+
233+
- (void)didCLickedCleanSearchHistory:(id)sender {
234+
235+
[CSSearchModel cleanAllSearchHistory];
236+
[self initSearchHistoryView];
237+
}
238+
239+
#pragma mark -
240+
#pragma mark Search Data Request
241+
242+
- (void)refresh {
243+
244+
if(_isLoading)
245+
return;
246+
247+
[self requestDataWithPage:_currentPage];
248+
}
249+
250+
- (void)loadMore {
251+
252+
if(_isLoading)
253+
return;
254+
255+
if(_currentPage >= _totalPage)
256+
return;
257+
258+
[self requestDataWithPage:_currentPage + 1];
259+
}
260+
261+
- (void)requestDataWithPage:(NSInteger)page {
262+
263+
if(page < 1)
264+
page = 1;
265+
266+
_isLoading = YES;
267+
268+
__weak typeof(self) weakSelf = self;
269+
[[Coding_NetAPIManager sharedManager] request_Tweet_WithSearchString:self.searchBar.text andPage:page andBlock:^(id data, NSError *error) {
270+
271+
if(data) {
272+
273+
NSDictionary *dataDic = (NSDictionary *)data;
274+
weakSelf.currentPage = [[dataDic valueForKey:@"page"] intValue];
275+
weakSelf.totalPage = [[dataDic valueForKey:@"totalPage"] intValue];
276+
NSArray *resultA = [NSObject arrayFromJSON:[dataDic objectForKey:@"list"] ofObjects:@"Tweet"];
277+
[weakSelf.tweetsArr addObjectsFromArray:resultA];
278+
[weakSelf.searchTableView reloadData];
279+
[weakSelf.searchTableView.infiniteScrollingView stopAnimating];
280+
weakSelf.searchTableView.showsInfiniteScrolling = weakSelf.currentPage >= weakSelf.totalPage ? NO : YES;
281+
}
282+
283+
weakSelf.isLoading = NO;
284+
}];
285+
}
286+
287+
#pragma mark -
288+
#pragma mark UISearchBarDelegate Support
289+
290+
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
291+
292+
[CSSearchModel addSearchHistory:searchBar.text];
293+
[self initSearchHistoryView];
294+
295+
// if(!_searchTableView) {
296+
//
297+
// [self initSearchResultsTableView];
298+
// }
299+
// else {
300+
//
301+
// [self.searchBar.superview bringSubviewToFront:_searchTableView];
302+
// }
303+
//
304+
// [searchBar resignFirstResponder];
305+
}
306+
307+
#pragma mark -
308+
#pragma mark UITableViewDelegate & UITableViewDataSource Support
309+
310+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
311+
312+
return [_tweetsArr count];
313+
}
314+
315+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
316+
317+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Search forIndexPath:indexPath];
318+
Tweet *tweet = _tweetsArr[indexPath.row];
319+
cell.textLabel.text = tweet.content;
320+
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:0];
321+
return cell;
322+
}
323+
324+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
325+
326+
return 80.0f;
327+
}
328+
329+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
330+
123331
}
124332

125333
@end

Coding_iOS/Controllers/Search/CSSearchModel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414
+ (BOOL)hasSearchBadgeShown;
1515
+ (void)invalidSearchBadge;
1616

17+
+ (NSArray *)getSearchHistory;
18+
+ (void)addSearchHistory:(NSString *)searchString;
19+
+ (void)cleanAllSearchHistory;
20+
1721
@end

0 commit comments

Comments
 (0)