效果图
//创建属性
@interfaceDFWSearchViewController()
@property(nonatomic,strong)UISearchBar*searchBar;
@property(nonatomic,strong)UICollectionView*myCollectionView;
@property(nonatomic,strong)NSMutableArray*searchHistoryArray;
@end
//.m UI布局
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorwhiteColor];
//
NSUserDefaults*user = [NSUserDefaultsstandardUserDefaults];
_searchHistoryArray= [usermutableArrayValueForKey:@"historyArray"];
//
self.navView.hidden=YES;
_searchBar= [[UISearchBaralloc]initWithFrame:CGRectMake(0,20,kScreenWidth,40)];
_searchBar.searchBarStyle=UISearchBarStyleMinimal;
_searchBar.showsCancelButton=YES;
_searchBar.placeholder=@"请输入搜索内容";
_searchBar.delegate=self;
_searchBar.keyboardType=UIKeyboardTypeNamePhonePad;
[self.viewaddSubview:_searchBar];
//
UILabel*historyLabel = [[UILabelalloc]initWithFrame:CGRectMake(20,CGRectGetMaxY(_searchBar.frame) +10,100,20)];
historyLabel.text=@"搜索历史";
[self.viewaddSubview:historyLabel];
UIButton*deleteBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(kScreenWidth-35,CGRectGetMinY(historyLabel.frame),15,15);
[deleteBtnsetImage:[UIImageimageNamed:@"delete"]forState:UIControlStateNormal];
[deleteBtnaddTarget:selfaction:@selector(delegateBtnAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:deleteBtn];
//
UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayoutalloc]init];
layout.scrollDirection=UICollectionViewScrollDirectionVertical;
layout.itemSize=CGSizeMake(80,30);
layout.sectionInset=UIEdgeInsetsMake(1,1,1,1);
layout.minimumInteritemSpacing=1;
layout.minimumLineSpacing=1;
self.myCollectionView= [[UICollectionViewalloc]initWithFrame:CGRectMake(20,CGRectGetMaxY(historyLabel.frame) +10,kScreenWidth-40,kScreenHeight-CGRectGetMaxY(historyLabel.frame) -10)collectionViewLayout:layout];
self.myCollectionView.delegate=self;
self.myCollectionView.dataSource=self;
self.myCollectionView.backgroundColor= [UIColorclearColor];
//注册item
[self.myCollectionViewregisterClass:[SearchHistoryCollectionViewCellclass]forCellWithReuseIdentifier:@"kitemCellid"];
[self.viewaddSubview:self.myCollectionView];
}
//以下是本文重点,每搜索一次 把搜索的文本存入本地。判断是否重复。
- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar
{
NSUserDefaults*user = [NSUserDefaultsstandardUserDefaults];
NSMutableArray*searchArray = [userobjectForKey:@"historyArray"];
if(!searchArray) {
NSMutableArray*searchArray = [NSMutableArraynew];
NSMutableArray*mutableCopyArr = [searchArraymutableCopy];
[mutableCopyArraddObject:searchBar.text];
[usersetObject: mutableCopyArrforKey:@"historyArray"];
NSString*str =searchBar.text;
}else{
NSMutableArray*mutableCopyArr = [searchArraymutableCopy];
BOOLisbool = [mutableCopyArrcontainsObject: searchBar.text];
if(isbool ==YES) {
NSLog(@"已存储%@", mutableCopyArr);
}else{
[mutableCopyArraddObject:searchBar.text];
[usersetObject: mutableCopyArrforKey:@"historyArray"];
}
NSString*str =searchBar.text;
}
[self.myCollectionViewreloadData];
[self.viewendEditing:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar*)searchBar{
NSLog(@"取消");
searchBar.text=@"";
[searchBarresignFirstResponder];
[self.navigationControllerpopViewControllerAnimated:YES];
}
//剩下的就是实现collectionview了
#pragma mark - UICollectionViewDelegate , UICollectionViewDataSource 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
return1;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath {
returnCGSizeMake(70,20);
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return_searchHistoryArray.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
//我再这个地方粗心了,直接用的系统的cell,导致文本字体重叠,最后自定义了cell解决了这个问题。
SearchHistoryCollectionViewCell*cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"kitemCellid"forIndexPath:indexPath];
cell.label.text= [NSStringstringWithFormat:@"%@", [self.searchHistoryArrayobjectAtIndex:indexPath.row]];
returncell;
}
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"搜索历史记录");
NSString*key = [NSStringstringWithFormat:@"%@", [self.searchHistoryArrayobjectAtIndex:indexPath.row]];
self.searchBar.text= key;
}
网友评论