-
Notifications
You must be signed in to change notification settings - Fork 3
/
weiboSearchFriendsViewViewController.m
239 lines (198 loc) · 7.74 KB
/
weiboSearchFriendsViewViewController.m
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
//
// weiboSearchFriendsViewViewController.m
// searchbarTest
//
// Created by 雨骁 刘 on 12-4-13.
// Copyright (c) 2012年 BUAA. All rights reserved.
//
#import "weiboSearchFriendsViewViewController.h"
#import "sqlService.h"
#import "WBSendView.h"
@interface weiboSearchFriendsViewViewController ()
@end
@implementation weiboSearchFriendsViewViewController
@synthesize theSearchBar;
@synthesize thedisplaycontroller;
@synthesize allItems;
@synthesize searchResults;
@synthesize instanceofTableView;
@synthesize PLViewController;
@synthesize mySendView;
@synthesize tempBtn;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//init the searchBar
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
theSearchBar.delegate = self;
theSearchBar.showsCancelButton = YES;
UIColor *barColor = [UIColor colorWithRed:9/255.0 green:125/255.0 blue:244/255.0 alpha:1.0];
theSearchBar.tintColor = barColor;
[theSearchBar setPlaceholder:@"谁了?"];
UITextField *tempTextField = [[UITextField alloc]init];
tempBtn = [[UIButton alloc] init];
for(UIView *v in [theSearchBar subviews])
{
NSLog(@"the subview of UISearchBar is %@",v);
if([v isKindOfClass:[UITextField class]])
{
tempTextField = (UITextField*)v;
}
if ([v isKindOfClass:[UIButton class]]) {
tempBtn = [(UIButton *)v retain];
}
}
// [tempBtn addTarget:self action:@selector(onCancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[tempTextField setKeyboardType:UIKeyboardTypeTwitter];
if (tempTextField) {
UIImage *image = [UIImage imageWithContentsOfFile: @"/Library/Application Support/Photo2Weibo/character1.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(0, 0, 30, 30);
iView.contentMode = UIViewContentModeScaleAspectFit;
tempTextField.leftView = iView;
[iView release];
}
[self.view addSubview:theSearchBar];
//init the displayView
thedisplaycontroller = [[UISearchDisplayController alloc] initWithSearchBar:theSearchBar contentsController:self];
thedisplaycontroller.delegate = self;
thedisplaycontroller.searchResultsDelegate = self;
thedisplaycontroller.searchResultsDataSource = self;
[thedisplaycontroller setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
instanceofTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 440) style:UITableViewStylePlain];
[instanceofTableView setDelegate:self];
[instanceofTableView setDataSource:self];
[self.view addSubview:instanceofTableView];
//init the allItems
sqlService *sqlSer = [[sqlService alloc] init];
NSMutableArray *temparray= [sqlSer getweibofriendList];
// NSLog(@"the temparray friends list is %@",temparray);
if([temparray count] == 0){
NSLog(@"error when read friends list");
}
else{
allItems = [[NSArray alloc] initWithArray:temparray];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"cancel button press-----------");
[mySendView setHidden: NO];
[self dismissViewControllerAnimated:NO
completion:^{
}];
[mySendView textviewFirstResponder];
}
- (void)onCancelSearch:(id)sender
{
[mySendView setHidden: NO];
[self dismissViewControllerAnimated:NO
completion:^{
}];
[mySendView textviewFirstResponder];
}
- (void)dealloc
{
[thedisplaycontroller setDelegate:nil], thedisplaycontroller = nil;
[theSearchBar setDelegate:nil],theSearchBar = nil;
[instanceofTableView setDelegate: nil];
[instanceofTableView release];
[tempBtn release];
instanceofTableView = nil;
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
[thedisplaycontroller setDelegate:nil], thedisplaycontroller = nil;
[theSearchBar setDelegate:nil],theSearchBar = nil;
}
#pragma mark TableView Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
return [self.searchResults count];
}
else {
return [allItems count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// NSUInteger section = [indexPath section];
// NSUInteger row = [indexPath row];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SectionsTableIdentifier];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
if([indexPath length]){
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}
}
else {
if([indexPath length]){
cell.textLabel.text = [allItems objectAtIndex:indexPath.row];
}
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = tempCell.textLabel.text;
//append it to sendview contenttextview
NSRange myRange = [cellText rangeOfString:@"("];
if(myRange.length >0 )
{
[mySendView appendContent: [[cellText substringToIndex:myRange.location] stringByAppendingString:@" "]];
}
else
{
[mySendView appendContent: [cellText stringByAppendingString:@" "]];
}
[mySendView setHidden: NO];
[self dismissViewControllerAnimated:NO
completion:^{
}];
[mySendView textviewFirstResponder];
}
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.searchResults = [allItems filteredArrayUsingPredicate:resultPredicate];
}
#pragma mark UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text]
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end