导读
效果:
主要代码:
[Objective-C] 纯文本查看 复制代码 #import "SearchVC.h"
#import "CusSearchBar.h"
#import "SearchRecordView.h"
@interface SearchVC (){
CusSearchBar *vSearch;
//猜你喜欢、搜索记录
SearchRecordView *vRecord;
NSArray *aryLike;
NSArray *aryRecord;
NSString *strKeyWord;
}
@end
@implementation SearchVC
-(void)initData{
aryRecord = [Utility getSearchRecordStringArr];
aryLike = @[@"平台设计",@"计算机",@"销售管理",@"总经理"];
}
- (void)viewDidLoad {
[super viewDidLoad];
__weak typeof(self)weakSelf = self;
vSearch =[[CusSearchBar alloc] initWithFrame:CGRectMake(10, 30, Width(260), 28) DidClickSearch:^(NSString *searchText) {
[weakSelf didClickSearchAction:searchText];
}];
vSearch.didBeginInput = ^{
[weakSelf didBeginEdit];
};
vSearch.textField.placeholder = @"请输入关键字";
[vSearch.textField setPlaceHolderColor:Color_ccc];
vSearch.textField.font = kFont(13);
vSearch.imageSize = 18;
vSearch.tintColor = Color_Blue;
vSearch.layer.cornerRadius = vSearch.frameHeight/2;
vSearch.backgroundColor = Color_fff;
// _vSearch.imgSearch.image = kim;
// vSearch.SearchTextAlignmentMiddle = YES;
[vSearch.textField becomeFirstResponder];
self.navigationItem.titleView = vSearch;
[self initSearchRecordView];
[self initRightBarButton];
[self initData];
}
#pragma mark - 开始编辑
-(void)didBeginEdit{
vRecord.hidden = NO;
aryRecord = [Utility getSearchRecordStringArr];
[vRecord updateViewByLikeArray:aryLike RecordArray:aryRecord];
}
#pragma mark - 搜索事件
-(void)didClickSearchAction:(NSString *)strKey{
vSearch.textField.text = strKey;
[vSearch.textField resignFirstResponder];
if (isNil(strKey)) {
// showInfoTips(@"请先输入关键字");
}else{
strKeyWord = strKey;
[Utility saveSearchRecordByAddKeyString:strKey];
vRecord.hidden = YES;
self.pageIndex = 1;
[self requestSearchResumeShowHud:YES];
}
}
-(void)initSearchRecordView{
__weak typeof(self)weakSelf = self;
vRecord = [[SearchRecordView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, FrameHeight)];
vRecord.aryRecord = aryRecord;
vRecord.aryLike = aryLike;
vRecord.didClickSearch = ^(NSString *strKey, NSInteger type) {
[weakSelf didClickSearchAction:strKey];
};
[self.view addSubview:vRecord];
}
-(void)initRightBarButton{
UIBarButtonItem *btmCancel = [self setBarButtonItem:@"搜索" ButtonTag:0 ButtonSize:CGSizeMake(40, 40)];
__weak __typeof (vSearch)weakSearch = vSearch;
__weak typeof(self)weakSelf = self;
[self addButtonItemClick:^(UIButton *sender) {
[weakSelf didClickSearchAction:weakSearch.textField.text];
}];
self.navigationItem.rightBarButtonItems = @[btmCancel,spaceButtonItem(10)];
}
#pragma mark - 取消
-(void)btmCancelAction{
[vSearch.textField resignFirstResponder];
if (isNil(strKeyWord)) {
popViewController(self, YES);
}else{
vRecord.hidden = YES;
}
}
#pragma mark - 数据请求
//猜你喜欢
-(void)requestGetLikeList{
}
//搜索
-(void)requestSearchResumeShowHud:(BOOL)showHud{
}
说明:搜索后的视图是个列表视图,只需要根据自己的需求,在Search.m里面实现TableView的相关代理协议即可。搜索记录是保存本地的。猜你喜欢是需要从服务器获取的,可实现requestGetLikeList方法,在相关地方进行调用。
附件:
搜索页带记录.zip
(754.62 KB, 下载次数: 2)
|