@interface SHSeachViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *listArr;
@property(nonatomic, retain)UISearchBar *searchBar;
@end
- (void)viewDidLoad {
[self createSearchBar];
}
- (void)createSearchBar {
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 260, 40);
self.navigationItem.titleView = self.searchBar;
self.searchBar.delegate = self;
self.searchBar.placeholder = @"搜索";
self.searchBar.barStyle = UIBarMetricsCompact;
self.seachBar.translucent = YES;
self.seachBar.showsSearchResultsButton = YES;
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(30, 0);
[_searchBar release];
}
#pragma mark --searchBar协议方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *searchStr = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *strUrl = [NSString stringWithFormat@"http://........%@...", searchStr];
[self createDataWithUrl:strUrl];
}
- (void)createDataWithUrl:(NSString *)strUrl {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = responseObject;
self.listArr = [SHModel baseModelByArr:dic[@"result"][@"list"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
- (UITableViewCell *)tableView:(UItableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
SHSearchTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[SHSearchTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
}
SHModel *model = self.listArr[indexPath.row];
NSString *keyword = self.searchBar.text;
keyword = [keyword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:model.name];
NSRange range = [model.name rangeOfString:keyword];
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:20]} range:range];
cell.titleLabel.text = model.name;
cell.titleLabel上
cell.titleLabel.attributedText = attrituteString;
return cell;
}