Skip to content

Commit 3359f5e

Browse files
committed
项目搜索-UISearchBar
1 parent 889bcad commit 3359f5e

11 files changed

Lines changed: 253 additions & 37 deletions

Coding_iOS.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
4EE083D91ADB72AA00CA342E /* bubble.html in Resources */ = {isa = PBXBuildFile; fileRef = 4EE083D71ADB72AA00CA342E /* bubble.html */; };
272272
4EE083DA1ADB72AA00CA342E /* topic-ios.html in Resources */ = {isa = PBXBuildFile; fileRef = 4EE083D81ADB72AA00CA342E /* topic-ios.html */; };
273273
4EE083DC1ADB736800CA342E /* service_terms.html in Resources */ = {isa = PBXBuildFile; fileRef = 4EE083DB1ADB736800CA342E /* service_terms.html */; };
274+
4EE5481E1AE7B7CA00A92306 /* UISearchBar+Common.m in Sources */ = {isa = PBXBuildFile; fileRef = 4EE5481D1AE7B7CA00A92306 /* UISearchBar+Common.m */; };
274275
68D9A2AD062641F5BCA573C5 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B024F2227BD4152BC7C9A7A /* libPods.a */; };
275276
8E03D4A219F695FE0092F8C8 /* libMobClickLibrary.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E03D49F19F695FE0092F8C8 /* libMobClickLibrary.a */; };
276277
8E03D4A419F696970092F8C8 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E03D4A319F696970092F8C8 /* libz.dylib */; };
@@ -1024,6 +1025,8 @@
10241025
4EE083D71ADB72AA00CA342E /* bubble.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = bubble.html; path = webview/app/build/bubble.html; sourceTree = "<group>"; };
10251026
4EE083D81ADB72AA00CA342E /* topic-ios.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = "topic-ios.html"; path = "webview/app/build/topic-ios.html"; sourceTree = "<group>"; };
10261027
4EE083DB1ADB736800CA342E /* service_terms.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = service_terms.html; sourceTree = "<group>"; };
1028+
4EE5481C1AE7B7CA00A92306 /* UISearchBar+Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UISearchBar+Common.h"; sourceTree = "<group>"; };
1029+
4EE5481D1AE7B7CA00A92306 /* UISearchBar+Common.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UISearchBar+Common.m"; sourceTree = "<group>"; };
10271030
8B18B0211092D3903D031B13 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
10281031
8E03D49F19F695FE0092F8C8 /* libMobClickLibrary.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libMobClickLibrary.a; sourceTree = "<group>"; };
10291032
8E03D4A019F695FE0092F8C8 /* MobClick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MobClick.h; sourceTree = "<group>"; };
@@ -1984,6 +1987,8 @@
19841987
4EBDA87B1A6640340035ED96 /* UIActionSheet+Common.m */,
19851988
4EB0C1FE1A807ED00042FC4F /* NSURL+Common.h */,
19861989
4EB0C1FF1A807ED00042FC4F /* NSURL+Common.m */,
1990+
4EE5481C1AE7B7CA00A92306 /* UISearchBar+Common.h */,
1991+
4EE5481D1AE7B7CA00A92306 /* UISearchBar+Common.m */,
19871992
);
19881993
path = OC_Category;
19891994
sourceTree = "<group>";
@@ -3525,6 +3530,7 @@
35253530
4EABD2571AD3CAAC005E515F /* UIMessageInputView_Add.m in Sources */,
35263531
8E59F0E21A0098BA009A905F /* UIScrollView+SVInfiniteScrolling.m in Sources */,
35273532
4E15C7D01A26D2F000FB8DAD /* FolderToMoveViewController.m in Sources */,
3533+
4EE5481E1AE7B7CA00A92306 /* UISearchBar+Common.m in Sources */,
35283534
4EA7F1621A6D192B00A046BD /* UIImage+GIF.m in Sources */,
35293535
8EF643BB19FF7E2900F7EEB0 /* MessageCell.m in Sources */,
35303536
8E1C3E0B19E8DFE300EF3032 /* SettingMineInfoViewController.m in Sources */,

Coding_iOS/Coding_iOS-Prefix.pch.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "UIBarButtonItem+Common.h"
3232
#import "UIActionSheet+Common.h"
3333
#import "NSURL+Common.h"
34+
#import "UISearchBar+Common.h"
3435
#import "UITTTAttributedLabel.h"
3536
#import "NSObject+ObjectMap.h"
3637
#import "ImageSizeManager.h"

Coding_iOS/Controllers/AddUserViewController.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ - (void)viewDidLoad {
4848
searchBar.delegate = self;
4949
[searchBar sizeToFit];
5050
[searchBar setPlaceholder:@"姓名/个性后缀"];
51-
searchBar.backgroundColor = [UIColor colorWithHexString:@"0x28303b"];
5251
searchBar;
5352
});
5453
_myTableView.tableHeaderView = _mySearchBar;

Coding_iOS/Controllers/ProjectMemberListViewController.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ - (void)loadView{
5757
searchBar.delegate = self;
5858
[searchBar sizeToFit];
5959
[searchBar setPlaceholder:@"姓名/个性后缀"];
60-
searchBar.backgroundColor = [UIColor colorWithHexString:@"0x28303b"];
6160
searchBar;
6261
});
6362
_myTableView.tableHeaderView = _mySearchBar;

Coding_iOS/Controllers/RootControllers/Project_RootViewController.m

Lines changed: 173 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616
#import "RDVTabBarController.h"
1717
#import "RDVTabBarItem.h"
1818
#import "NProjectViewController.h"
19+
#import "ProjectListCell.h"
1920

20-
@interface Project_RootViewController ()
21+
22+
@interface Project_RootViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
2123
@property (strong, nonatomic) XTSegmentControl *mySegmentControl;
2224
@property (strong, nonatomic) iCarousel *myCarousel;
2325
@property (strong, nonatomic) NSMutableDictionary *myProjectsDict;
2426
@property (assign, nonatomic) NSInteger oldSelectedIndex;
27+
28+
@property (strong, nonatomic) UISearchBar *mySearchBar;
29+
@property (strong, nonatomic) UISearchDisplayController *mySearchDisplayController;
30+
31+
@property (strong, nonatomic) NSMutableArray *searchResults;
32+
@property (strong, nonatomic) NSString *searchString;
33+
2534
@end
2635

2736
@implementation Project_RootViewController
@@ -85,17 +94,11 @@ - (void)viewDidLoad
8594
}
8695

8796
- (void)setupNavBtn{
88-
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchItemClicked:)];
97+
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchItemClicked:)];
8998
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(gotoNewProject)];
9099

91100
}
92101

93-
-(void)gotoNewProject{
94-
UIStoryboard *newProjectStoryboard = [UIStoryboard storyboardWithName:@"NewProject" bundle:nil];
95-
UIViewController *newProjectVC = [newProjectStoryboard instantiateViewControllerWithIdentifier:@"NewProjectVC"];
96-
[self.navigationController pushViewController:newProjectVC animated:YES];
97-
}
98-
99102
- (void)configSegmentItems{
100103
_segmentItems = @[@"全部项目", @"我参与的", @"我创建的"];
101104
}
@@ -137,19 +140,7 @@ - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
137140
}else{
138141
__weak Project_RootViewController *weakSelf = self;
139142
listView = [[ProjectListView alloc] initWithFrame:carousel.bounds projects:curPros block:^(Project *project) {
140-
if (curPros.type < ProjectsTypeTaProject) {
141-
[[Coding_NetAPIManager sharedManager] request_Project_UpdateVisit_WithObj:project andBlock:^(id data, NSError *error) {
142-
if (data) {
143-
project.un_read_activities_count = [NSNumber numberWithInteger:0];
144-
[listView refreshUI];
145-
}
146-
}];
147-
}
148-
149-
NProjectViewController *vc = [[NProjectViewController alloc] init];
150-
vc.myProject = project;
151-
[weakSelf.navigationController pushViewController:vc animated:YES];
152-
143+
[weakSelf goToProject:project];
153144
NSLog(@"\n=====%@", project.name);
154145
} tabBarHeight:CGRectGetHeight(self.rdv_tabBarController.tabBar.frame)];
155146
}
@@ -186,9 +177,170 @@ - (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
186177
}];
187178
}
188179

180+
#pragma mark VC
181+
-(void)gotoNewProject{
182+
UIStoryboard *newProjectStoryboard = [UIStoryboard storyboardWithName:@"NewProject" bundle:nil];
183+
UIViewController *newProjectVC = [newProjectStoryboard instantiateViewControllerWithIdentifier:@"NewProjectVC"];
184+
[self.navigationController pushViewController:newProjectVC animated:YES];
185+
}
186+
187+
- (void)goToProject:(Project *)project{
188+
Projects *curPros = [_myProjectsDict objectForKey:[NSNumber numberWithUnsignedInteger:_myCarousel.currentItemIndex]];
189+
ProjectListView *listView = (ProjectListView *)self.myCarousel.currentItemView;
190+
if (curPros.type < ProjectsTypeTaProject) {
191+
[[Coding_NetAPIManager sharedManager] request_Project_UpdateVisit_WithObj:project andBlock:^(id data, NSError *error) {
192+
if (data) {
193+
project.un_read_activities_count = [NSNumber numberWithInteger:0];
194+
[listView refreshUI];
195+
}
196+
}];
197+
}
198+
NProjectViewController *vc = [[NProjectViewController alloc] init];
199+
vc.myProject = project;
200+
[self.navigationController pushViewController:vc animated:YES];
201+
}
202+
189203
#pragma mark Search
190204
- (void)searchItemClicked:(id)sender{
191205
NSLog(@"%@", sender);
206+
if (!_mySearchBar) {
207+
_mySearchBar = ({
208+
UISearchBar *searchBar = [[UISearchBar alloc] init];
209+
searchBar.delegate = self;
210+
[searchBar sizeToFit];
211+
[searchBar setPlaceholder:@"项目名称/创建人"];
212+
[searchBar setTintColor:[UIColor whiteColor]];
213+
[searchBar insertBGColor:[UIColor colorWithHexString:@"0x28303b"]];
214+
searchBar;
215+
});
216+
[self.navigationController.view addSubview:_mySearchBar];
217+
[_mySearchBar setY:20];
218+
}
219+
if (!_mySearchDisplayController) {
220+
_mySearchDisplayController = ({
221+
UISearchDisplayController *searchVC = [[UISearchDisplayController alloc] initWithSearchBar:_mySearchBar contentsController:self];
222+
searchVC.searchResultsTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.mySearchBar.frame), 0, CGRectGetHeight(self.rdv_tabBarController.tabBar.frame), 0);
223+
searchVC.searchResultsTableView.tableFooterView = [[UIView alloc] init];
224+
[searchVC.searchResultsTableView registerClass:[ProjectListCell class] forCellReuseIdentifier:kCellIdentifier_ProjectList];
225+
searchVC.searchResultsDataSource = self;
226+
searchVC.searchResultsDelegate = self;
227+
if (kHigher_iOS_6_1) {
228+
searchVC.displaysSearchBarInNavigationBar = NO;
229+
}
230+
searchVC;
231+
});
232+
}
233+
234+
[_mySearchBar becomeFirstResponder];
235+
}
236+
237+
#pragma mark Table
238+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
239+
return 1;
240+
}
241+
242+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
243+
if (self.searchResults) {
244+
return [self.searchResults count];
245+
}else{
246+
return 0;
247+
}
248+
}
249+
250+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
251+
ProjectListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectList forIndexPath:indexPath];
252+
cell.project = [self.searchResults objectAtIndex:indexPath.row];
253+
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
254+
return cell;
255+
256+
}
257+
258+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
259+
return [ProjectListCell cellHeight];
260+
}
261+
262+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
263+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
264+
[self.mySearchBar resignFirstResponder];
265+
[self goToProject:[self.searchResults objectAtIndex:indexPath.row]];
266+
}
267+
268+
#pragma mark UISearchBarDelegate
269+
270+
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
271+
NSLog(@"textDidChange: %@", searchText);
272+
[self searchProjectWithStr:searchText];
273+
}
274+
275+
276+
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
277+
NSLog(@"searchBarSearchButtonClicked: %@", searchBar.text);
278+
[self searchProjectWithStr:searchBar.text];
279+
}
280+
281+
- (void)searchProjectWithStr:(NSString *)string{
282+
self.searchString = string;
283+
[self updateFilteredContentForSearchString:string];
284+
[self.mySearchDisplayController.searchResultsTableView reloadData];
285+
}
286+
287+
- (void)updateFilteredContentForSearchString:(NSString *)searchString{
288+
// start out with the entire list
289+
Projects *curPros = [_myProjectsDict objectForKey:@0];
290+
if (curPros) {
291+
self.searchResults = [curPros.list mutableCopy];
292+
}else{
293+
self.searchResults = nil;
294+
}
295+
296+
// strip out all the leading and trailing spaces
297+
NSString *strippedStr = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
298+
299+
// break up the search terms (separated by spaces)
300+
NSArray *searchItems = nil;
301+
if (strippedStr.length > 0)
302+
{
303+
searchItems = [strippedStr componentsSeparatedByString:@" "];
304+
}
305+
306+
// build all the "AND" expressions for each value in the searchString
307+
NSMutableArray *andMatchPredicates = [NSMutableArray array];
308+
309+
for (NSString *searchString in searchItems)
310+
{
311+
// each searchString creates an OR predicate for: name, global_key
312+
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
313+
314+
// name field matching
315+
NSExpression *lhs = [NSExpression expressionForKeyPath:@"name"];
316+
NSExpression *rhs = [NSExpression expressionForConstantValue:searchString];
317+
NSPredicate *finalPredicate = [NSComparisonPredicate
318+
predicateWithLeftExpression:lhs
319+
rightExpression:rhs
320+
modifier:NSDirectPredicateModifier
321+
type:NSContainsPredicateOperatorType
322+
options:NSCaseInsensitivePredicateOption];
323+
[searchItemsPredicate addObject:finalPredicate];
324+
325+
// owner_user_name field matching
326+
lhs = [NSExpression expressionForKeyPath:@"owner_user_name"];
327+
rhs = [NSExpression expressionForConstantValue:searchString];
328+
finalPredicate = [NSComparisonPredicate
329+
predicateWithLeftExpression:lhs
330+
rightExpression:rhs
331+
modifier:NSDirectPredicateModifier
332+
type:NSContainsPredicateOperatorType
333+
options:NSCaseInsensitivePredicateOption];
334+
[searchItemsPredicate addObject:finalPredicate];
335+
336+
// at this OR predicate to ourr master AND predicate
337+
NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
338+
[andMatchPredicates addObject:orMatchPredicates];
339+
}
340+
341+
NSCompoundPredicate *finalCompoundPredicate = (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
342+
343+
self.searchResults = [[self.searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy];
192344
}
193345

194346
@end

Coding_iOS/Controllers/TweetSendLocationViewController.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,13 @@ - (void)dismissSelf{
477477

478478
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
479479
{
480+
[searchBar insertBGColor:[UIColor colorWithHexString:@"0x28303b"]];
480481
[self.mySearchDisplayController setActive:YES animated:YES];
481-
482+
return YES;
483+
}
484+
485+
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
486+
[searchBar insertBGColor:nil];
482487
return YES;
483488
}
484489

Coding_iOS/Controllers/UsersViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "Users.h"
1111
#import "PrivateMessage.h"
1212

13-
@interface UsersViewController : BaseViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate,UISearchDisplayDelegate>
13+
@interface UsersViewController : BaseViewController
1414
@property (strong, nonatomic) Users *curUsers;
1515
@property (strong, nonatomic) PrivateMessage *curMessage;
1616
@property (copy, nonatomic) void(^selectUserBlock)(User *selectedUser);

Coding_iOS/Controllers/UsersViewController.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#import "SVPullToRefresh.h"
1717
#import "AddUserViewController.h"
1818

19-
@interface UsersViewController ()
19+
@interface UsersViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate,UISearchDisplayDelegate>
2020
@property (strong, nonatomic) UISearchBar *mySearchBar;
2121
@property (strong, nonatomic) UISearchDisplayController *mySearchDisplayController;
2222
@property (strong, nonatomic) UITableView *myTableView;
@@ -95,8 +95,6 @@ - (void)viewDidLoad{
9595
searchBar.delegate = self;
9696
[searchBar sizeToFit];
9797
[searchBar setPlaceholder:@"姓名/个性后缀"];
98-
searchBar.backgroundColor = [UIColor colorWithHexString:@"0xe5e5e5"];
99-
10098
searchBar;
10199
});
102100
_myTableView.tableHeaderView = _mySearchBar;
@@ -368,6 +366,16 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
368366
}
369367

370368

369+
#pragma mark UISearchBarDelegate
370+
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
371+
[searchBar insertBGColor:[UIColor colorWithHexString:@"0x28303b"]];
372+
return YES;
373+
}
374+
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
375+
[searchBar insertBGColor:nil];
376+
return YES;
377+
}
378+
371379
#pragma mark UISearchDisplayDelegate M
372380
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
373381
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// UISearchBar+Common.h
3+
// Coding_iOS
4+
//
5+
// Created by Ease on 15/4/22.
6+
// Copyright (c) 2015年 Coding. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UISearchBar (Common)
12+
- (void)insertBGColor:(UIColor *)backgroundColor;
13+
14+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// UISearchBar+Common.m
3+
// Coding_iOS
4+
//
5+
// Created by Ease on 15/4/22.
6+
// Copyright (c) 2015年 Coding. All rights reserved.
7+
//
8+
9+
#import "UISearchBar+Common.h"
10+
11+
@implementation UISearchBar (Common)
12+
- (void)insertBGColor:(UIColor *)backgroundColor{
13+
static NSInteger customBgTag = 999;
14+
UIView *realView = [[self subviews] firstObject];
15+
[[realView subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
16+
if (obj.tag == customBgTag) {
17+
[obj removeFromSuperview];
18+
}
19+
}];
20+
if (backgroundColor) {
21+
UIImageView *customBg = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:backgroundColor withFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) + 20)]];
22+
[customBg setY:-20];
23+
customBg.tag = customBgTag;
24+
[[[self subviews] firstObject] insertSubview:customBg atIndex:1];
25+
}
26+
}
27+
@end

0 commit comments

Comments
 (0)