forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSTopicCreateVC.m
More file actions
365 lines (303 loc) · 12.5 KB
/
CSTopicCreateVC.m
File metadata and controls
365 lines (303 loc) · 12.5 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// CSTopicCreateVC.m
// Coding_iOS
//
// Created by pan Shiyu on 15/7/17.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "CSTopicCreateVC.h"
#import "CSTopicModel.h"
#import "Coding_NetAPIManager.h"
#define kCellIdentifier_TopicNameCell @"kCellIdentifier_TopicNameCell"
@interface CSTopicCreateVC () <UITableViewDataSource,UITableViewDelegate, UISearchBarDelegate,UISearchDisplayDelegate>
@property (nonatomic,strong)UITableView *listView;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,strong)UISearchDisplayController *mySearchDisplayController;
@property (nonatomic,copy)NSString *createdTopicName;
@property (nonatomic,strong)NSArray *historyTopiclist;
@property (nonatomic,strong)NSArray *hotTopiclist;
@end
@implementation CSTopicCreateVC
+ (void)showATSomeoneWithBlock:(void(^)(NSString *topicName))block{
CSTopicCreateVC *vc = [[CSTopicCreateVC alloc] init];
vc.selectTopicBlock = block;
UINavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
[[BaseViewController presentingVC] presentViewController:nav animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;
_listView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[CSTopicNameCell class] forCellReuseIdentifier:kCellIdentifier_TopicNameCell];
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
tableView.sectionIndexColor = kColor666;
tableView.sectionHeaderHeight = 20;
tableView.rowHeight = 44;
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[tableView setContentInset:UIEdgeInsetsZero];
[tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
tableView;
});
_searchBar = ({
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;
[searchBar sizeToFit];
[searchBar setPlaceholder:@"#新话题"];
searchBar.showsCancelButton = YES;
[searchBar setTintColor:[UIColor whiteColor]];
[searchBar insertBGColor:kColorNavBG];
searchBar.searchBarStyle = UISearchBarStyleDefault;
searchBar.translucent = YES;
searchBar.backgroundColor = [UIColor clearColor];
searchBar;
});
_mySearchDisplayController = ({
UISearchDisplayController *searchVC = [[CSMySearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
[searchVC.searchResultsTableView registerClass:[CSTopicNameCell class] forCellReuseIdentifier:kCellIdentifier_TopicNameCell];
searchVC.delegate = self;
searchVC.searchResultsDataSource = self;
searchVC.searchResultsDelegate = self;
// if (kHigher_iOS_6_1) {
// searchVC.displaysSearchBarInNavigationBar = NO;
// }
searchVC.displaysSearchBarInNavigationBar = YES;
searchVC;
});
_createdTopicName = nil;
_historyTopiclist = [CSTopicModel latestUseTopiclist];
_hotTopiclist = @[];
[self.listView reloadData];
[self refreshHotTopiclist];
}
- (void)refreshHotTopiclist{
__weak typeof(self) wself = self;
[[Coding_NetAPIManager sharedManager] request_DefautsHotTopicNamelistWithBlock:^(NSArray *nameList, NSError *error) {
wself.hotTopiclist = nameList.count > 10? [nameList subarrayWithRange:NSMakeRange(0, 10)]: nameList;
[wself.listView reloadData];
}];
}
- (void)dealloc
{
_listView.delegate = nil;
_listView.dataSource = nil;
}
#pragma mark -
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView == _mySearchDisplayController.searchResultsTableView) {
return nil;
}
if (section == 0) {
return @"最近使用";
}
if (section == 1) {
return @"热门推荐";
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (tableView == _mySearchDisplayController.searchResultsTableView) {
return 0;
}
if (section == 0 && _historyTopiclist.count == 0) {
return 0;
}
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
if (height <= 0) {
return nil;
}
if (section == 0 && _historyTopiclist.count == 0) {
return nil;
}
UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, height)];
headerV.backgroundColor = kColorTableSectionBg;
UILabel *titleL = [[UILabel alloc] init];
titleL.font = [UIFont systemFontOfSize:12];
titleL.textColor = kColor999;
titleL.text = [self tableView:tableView titleForHeaderInSection:section];
[headerV addSubview:titleL];
[titleL mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(headerV).insets(UIEdgeInsetsMake(4, kPaddingLeftWidth, 4, kPaddingLeftWidth));
}];
return headerV;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (tableView == _mySearchDisplayController.searchResultsTableView) {
if (_createdTopicName && _createdTopicName.length > 0) {
return 1;
}
return 0;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == _mySearchDisplayController.searchResultsTableView) {
if (_createdTopicName && _createdTopicName.length > 0) {
return 1;
}else{
return 0;
}
}
if (section == 0) {
return _historyTopiclist.count;
}
return _hotTopiclist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CSTopicNameCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_TopicNameCell forIndexPath:indexPath];
NSString *selectedTopicName = nil;
if (tableView == _mySearchDisplayController.searchResultsTableView) {
selectedTopicName = _createdTopicName;
[cell showCreateBtn:(_createdTopicName && _createdTopicName.length > 0)];
}else{
[cell showCreateBtn:NO];
if (indexPath.section == 0) {
selectedTopicName = _historyTopiclist[indexPath.row];
}else if(indexPath.section == 1){
selectedTopicName = _hotTopiclist[indexPath.row];
}
}
if (!selectedTopicName) {
selectedTopicName = @"";
}
cell.textLabel.text = [NSString stringWithFormat:@"#%@#",selectedTopicName];
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:12];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selectedTopicName = nil;
if (tableView == _mySearchDisplayController.searchResultsTableView) {
selectedTopicName = _createdTopicName;
}else{
if (indexPath.section == 0) {
selectedTopicName = _historyTopiclist[indexPath.row];
}else if(indexPath.section == 1){
selectedTopicName = _hotTopiclist[indexPath.row];
}
}
[self didSelectByTopicName:selectedTopicName];
}
- (void)didSelectByTopicName:(NSString*)topicName {
if (!topicName || topicName.length == 0) {
return;
}
[CSTopicModel addAnotherUseTopic:topicName];
//为了解决键盘状态混淆的问题
if ([self.searchBar isFirstResponder]) {
[self.searchBar resignFirstResponder];
}
__weak typeof(self) wself = self;
[self dismissViewControllerAnimated:YES completion:^{
if (wself.selectTopicBlock) {
wself.selectTopicBlock(topicName);
}
}];
}
#pragma mark -
#pragma mark UISearchBarDelegate
//- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView{
// [self.mySearchDisplayController.searchResultsTableView reloadData];
// NSLog(@"--willHideSearchResultsTableView--");
//}
//
//- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
// [self.mySearchDisplayController.searchResultsTableView reloadData];
// NSLog(@"--didShowSearchResultsTableView--");
//}
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
//解决UISearchDisplayController 顶部的空白区域的问题
[tableView setContentInset:UIEdgeInsetsZero];
[tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[_mySearchDisplayController.searchResultsTableView reloadData];
}
//解决不显示cancel按钮的问题
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
[_mySearchDisplayController setActive:YES animated:YES];
[_mySearchDisplayController.searchBar setShowsCancelButton:YES animated:YES];
controller.searchResultsTableView.superview.bounds = CGRectMake(0,22,320,400);
for(UIView * v in controller.searchResultsTableView.superview.subviews){
if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])
{
v.frame = CGRectMake(0,20,320,400);
//
}
}
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
[_mySearchDisplayController setActive:NO animated:YES];
[_mySearchDisplayController.searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *strippedStr = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
_createdTopicName = strippedStr;
[self.mySearchDisplayController.searchResultsTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
if ([searchBar isFirstResponder]) {
[searchBar resignFirstResponder];
}else{
[self dismissViewControllerAnimated:YES completion:^{
//
}];
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSString *strippedStr = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
_createdTopicName = strippedStr;
[self didSelectByTopicName:_createdTopicName];
}
@end
@implementation CSTopicNameCell{
UILabel *_createLabel;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textLabel.frame = CGRectMake(kPaddingLeftWidth, 0, kScreen_Width - kPaddingLeftWidth - 15, 44);
self.textLabel.font = [UIFont systemFontOfSize:15];
self.textLabel.textColor = kColor222;
self.textLabel.backgroundColor = [UIColor clearColor];
}
return self;
}
- (UILabel *)createLabel {
if (!_createLabel) {
_createLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
_createLabel.right = kScreen_Width - 15;
_createLabel.text = @"创建";
_createLabel.textColor = [UIColor colorWithHexString:@"0x7ccd9f"];
_createLabel.font = [UIFont systemFontOfSize:15];
_createLabel.textAlignment = NSTextAlignmentRight;
[self.contentView addSubview:_createLabel];
}
return _createLabel;
}
- (void)showCreateBtn:(BOOL)showCreateBtn {
[self createLabel].hidden = !showCreateBtn;
}
@end
@implementation CSMySearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
}
@end